Javascript Quick Reference | Software Engineering | Areas Of Computer Science

JavaScript Quick Reference

Variable Declaration Var bFound= false; var iCount= 0; var fTotal= 2.5; var sName = ""; //Boolean //Integer //Floating Point Number //String

www.kellermansoftware.com – Free Quick References and .net components.
Functions //Normal Declaration function sum(x,y) { return x + y; } //Optional Arguments function requiredString(s, bAlert) { var bValid= true; if (s.length == 0) bValid= false; Operators Computational Unary negation ++ Increment -Decrement * Multiplication / Division % Modulo arithmetic + Addition Subtraction Logical ! Logical NOT < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equality != Inequality && Logical AND || Logical OR ?: Conditional (unary) , Comma Bitwise ~ Bitwise NOT << Bitwise Shift Left >> Bitwise Shift Right >>> Unsigned Shift Right & Bitwise AND ^ Bitwise XOR | Bitwise OR Assignment = Assignment Compound assignment operators += Addition -= Subtraction *= Multiplication /= Division %= Modulo Arithmetic <<= Left Shift >>= Right Shift >>>= Unsigned Right Shift &= Bitwise And |= Bitwise Or ^= Bitwise Xor JavaScript Within Script Tag <script> function myFunction() {alert("Hi");} </script>

var dNow = new Date(); //Current Date var dPast= new Date(2002,5-1,20,23,59,59,999); // (year, month - 1, day, hours, minutes, seconds, milliseconds) var a = new Array(8); //Array

Loops //While Loop var x = 1; while ( x <= 10 ) { x++; } //For Loop for (var x = 1; x <= 10; x++ ) { } //Do While Loop var i = 0; do { document.write(i + ".<BR>"); i+=2; } while(i<20);

Common Events onClick onSubmit onMouseOver onMouseOut onFocus onChange onBlur onLoad onUnload onKeyDown onKeyPress Built-In Functions Escape Eval isNaN Number parseFloat parseInt String Unescape

//If bAlert is not passed in, //it will be equal to "undefined“ if (String(bAlert) == "true") alert("String is required"); } //Dynamic Arguments function sumx() { var iReturn=0; for (var i=0;i < arguments.length;i++) iReturn+= arguments[i]; return iReturn; }

Branching if ( (x == 1) && (y == 3) ) sum = y - x; else sum=0; switch(s) { case "A": alert("A Chosen"); break; case "B": alert("B Chosen"); break; // if all other conditions fail do this default; alert("Some other value chosen"); break; }

Alert, Prompt, Confirm window.alert("Hello Greg!"); sInput= prompt('Enter number to order','1'); if (confirm("Save before exiting?")) {}

Referencing Form Elements var sFirstName= document.forms[0].txtFirstName.value; var sLastName= document.frmMain.txtLastName.value; var sZip= document.forms[0].elements("txtZip").value;

Regular Expression Pattern Test var pat=/^\d{5}(-{0,1}\d{4}){0,1}$/ ; var sZip= “4322A” if(!pat.test(sZip)) //Invalid Zip Code {} Regular Expression Replace mask = /\s|[,]|[$]/g; //Currency Chars s=s.replace(mask,''); //Remove

Split Into Array var a,s; s="1,OH,2,IL"; a= s.split(",");

JavaScript in External Files (Most Efficient) <script defer language="Javascript" src="script/validate.js"></script>

JavaScript Web References http://www.devguru.com http://www.mattkruse.com/javascript/ http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/dhtml_reference_entry.asp String Methods toUpperCase, toLowerCase, substr, substring, indexOf

Popup Window var sConfig= 'left=100, top=0, height=500, width=400, menubar=0, scrollbars=0, status=0, toolbar=0, location=0'; var myWindow= window.open("http://myPopupWindow.html", "MyWindowName", sConfig);}

Sign up to vote on this title
UsefulNot useful