Wednesday, April 16, 2008

Convert integer to words

var units = new Array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
var tens = new Array ("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");

function num(it) {
var theword = "";
var started;
if (it>999) return "Lots";
if (it==0) return units[0];
for (var i = 9; i >= 1; i--){
if (it>=i*100) {
theword += units[i];
started = 1;
theword += " hundred";
if (it!=i*100) theword += " and ";
it -= i*100;
i=0;
}
};

for (var i = 9; i >= 2; i--){
if (it>=i*10) {
theword += (started?tens[i-2].toLowerCase():tens[i-2]);
started = 1;
if (it!=i*10) theword += "-";
it -= i*10;
i=0
}
};

for (var i=1; i < 20; i++) {
if (it==i) {
theword += (started?units[i].toLowerCase():units[i]);
}
};
return theword;
}

Javascript write & read cookie

Write Cookie
if(hours != null)

{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}

document.cookie = name + "=" + escape(value) + expire;


Read Cookie
var cookieValue = "";

var search = name + "=";

if(document.cookie.length > 0)

{

offset = document.cookie.indexOf(search);

if (offset != -1)

{

offset += search.length;

end = document.cookie.indexOf(";", offset);

if (end == -1) end = document.cookie.length;

cookieValue = unescape(document.cookie.substring(offset, end))

}

}

Friday, June 15, 2007

checked and unchecked all checkbox value

function prepare(form_id) {
var formblock;
formblock= document.getElementById(form_id);
forminputs = formblock.getElementsByTagName('input');
return forminputs;
}

function select_all(formname,name, value) {
forminputs = prepare(formname);
for (i = 0; i < forminputs.length; i++) {
// regex here to check name attribute
var regex = new RegExp(name, "i");
if (regex.test(forminputs[i].getAttribute('name'))) {
if (value == '1') {
forminputs[i].checked = true;
} else {
forminputs[i].checked = false;
}
}
}
}

if (window.addEventListener) {
window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
window.attachEvent("onload", prepare)
} else if (document.getElementById) {
window.onload = prepare;
}

Thursday, May 17, 2007

redirect parent when freame is on

top.location.href='../member.php'

Sunday, May 13, 2007

Maintaining Page Scroll Position

This is a small example how to maintain scroll position of the page/div during refresh with the help of several lines of javascript code:

< html>
< title>< /title>
< head>
< script language="javascript">

// function saves scroll position
function fScroll(val)
{
var hidScroll = document.getElementById('hidScroll');
hidScroll.value = val.scrollTop;
}

// function moves scroll position to saved value
function fScrollMove(what)
{
var hidScroll = document.getElementById('hidScroll');
document.getElementById(what).scrollTop = hidScroll.value;
}
< /script>
< /head>

< body onload="fScrollMove('div_scroll');" onunload="document.forms(0).submit()";>
< form>
< input type="text" id="hidScroll" name="a">< /br>
< div id="div_scroll" onscroll="fScroll(this);"
style="overflow:auto;height:100px;width:100px;">

.. VERY LONG TEXT GOES HERE

< /div>
< /form>
< /body>
< /html>


The example is supposed to be cross-browser compatible. Enjoy :)