Thursday, April 10, 2008

JS code to disable right click and refresh button

document.onmousedown="if (event.button==2) return false";
document.oncontextmenu=new Function("return false");//disable right click

document.onkeydown = showDown;

function showDown(evt)
{
evt = (evt)? evt : ((event)? event : null);
if (evt) {
if (event.keycode == 8 && (event.srcElement.type!= "text" && event.srcElement.type!= "textarea" && event.srcElement.type!= "password")) {
// When backspace is pressed but not in form element
cancelKey(evt);
}
else if (event.keycode == 116) {
// When F5 is pressed
alert("asdfasfdasd");
cancelKey(evt);
}
else if (event.keycode == 122) {
// When F11 is pressed
cancelKey(evt);
}
else if (event.ctrlKey && (event.keycode == 78 ¦¦ event.keycode == 82)) {
// When ctrl is pressed with R or N
cancelKey(evt);
}
else if (event.altKey && event.keycode==37 ) {
// stop Alt left cursor
return false;
}
}
}

function cancelKey(evt) {
if (evt.preventDefault) {
evt.preventDefault();
return false;
}
else {
evt.keycode = 0;
evt.returnValue = false;
}
}

Javascript code to assaign comma into number/currency

This js fuction can convert currency to commified number. To assaign comma in between number this function is very helpful.



function commify(str)
{
var Num = str
var newNum = "";
var newNum2 = "";
var count = 0;

//check for decimal number
if (Num.indexOf('.') != -1){ //number ends with a decimal point
if (Num.indexOf('.') == Num.length-1){
Num += "00";
}
if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit
Num += "0";
}

var a = Num.split(".");
Num = a[0]; //the part we will commify
var end = a[1] //the decimal place we will ignore and add back later
}
else {var end = "00";}

//this loop actually adds the commas
for (var k = Num.length-1; k >= 0; k--){
var oneChar = Num.charAt(k);
if (count == 3){
newNum += ",";
newNum += oneChar;
count = 1;
continue;
}
else {
newNum += oneChar;
count ++;
}
} //but now the string is reversed!

//re-reverse the string
for (var k = newNum.length-1; k >= 0; k--){
var oneChar = newNum.charAt(k);
newNum2 += oneChar;
}

// add dollar sign and decimal ending from above
newNum2 = newNum2 + "." + end;
return newNum2;
}

Javascript code for Converting english number to word

This function can convert english number to word like 59 to Fifty Nine. Also can handle fractional part. If one can use only zeros after point he must need to give only two zero like 2.00.



var th = ['','Thousand','Million', 'Billion','Trillion'];
var dg = ['Zero','One','Two','Three','Four', 'Five','Six','Seven','Eight','Nine'];
var tn = ['Ten','Eleven','Twelve','Thirteen', 'Fourteen','Fifteen','Sixteen', 'Seventeen','Eighteen','Nineteen'];
var tw = ['Twenty','Thirty','Forty','Fifty', 'Sixty','Seventy','Eighty','Ninety'];
function toWords(s)
{s = s.replace(/[\, ]/g,'');
var len=s.length;
var babu="";
if(len>2 && s.charAt(len-1)=='0'&& s.charAt(len-2)=='0' && s.charAt(len-3)=='.')
{
//alert(s);
for(var i=0;i 15)
return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i=0; i < x; i++)
{if ((x-i)%3==2)
{if (n[i] == '1')
{str += tn[Number(n[i+1])] + ' '; i++; sk=1;
}
else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}}
else if (n[i]!=0) {str += dg[n[i]] +' ';
if ((x-i)%3==0) str += 'Hundred ';sk=1;}
if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}}
if (x != s.length)
{var y = s.length; str += 'point ';
for (var i=x+1; i< y; i++)
str += dg[n[i]] +' ';
}
return str.replace(/\s+/g,' ');
}

Javascript code for table height

document.getElementById("your_table_id").offsetHeight

Wednesday, April 2, 2008

Some necessary JavaScript Code

JS code for List Box(multiple selecet) item add,delete,duplicate check


function list_box_add()
{
//'serial_list' is the name of list box
//'serial_number' is a comboBox
//the selected value of comboBox are adding to the list

/**********duplication check block***********/
for (i =0;i=0)
document.form1.serial_list.remove(document.forms["form1"].serial_list.selectedIndex);
else
alert('No Item Selected');

//alert(document.forms["form1"].serial_list.options[1]);
//document.forms["myForm"].state.options[1];
//document.write(document.forms['form1'].serial_list.selectedIndex);
}


JS code for delete row of table

function deleteRow(id)
{
var row = id.parentNode.parentNode.rowIndex;
var tbody = document.getElementById("table_id").tBodies[0].deleteRow(row);
//'table_id' is the id of the table
}

//calling function should call like this[ onclick="deleteRow(this)"] into button tag
JS code for trim string

function triim(st,d)//st string & d=1 for killing space inside the string

//d=0 & d=1 both kill leading & trailing space

{

var len = st.length;

var str="";

var i;

var first=0,last=len;

for(i=0;i=0;i--)

{ if(st.charAt(i)!=' ')

{ last=i;

break;

}

}

for(i=first;i<=last;i++) { if(d==1 && st.charAt(i)==' ') {} else str+=st.charAt(i); } return str; }
JS code to add new row in table

function add_row(){
var tbody = document.getElementById("table_id").getElementsByTagName("tbody")[0];
var row = document.createElement("TR");
var cell1 = document.createElement("TD");
cell1.innerHTML = "babu"; //can use some HTML

//var cell2 = document.createElement("TD");
//cell2.innerHTML = "babu"; //can use some HTML

/****** as required cell can b added*********/
row.appendChild(cell1);
//row.appendChild(cell2);
tbody.appendChild(row);
}


PC Magazine Tips and Solutions

PC World: Latest Technology News

PCWorld.com - Most Popular Downloads of the Week