JavaScript Syntax
Java may be inserted into an HTML document between the <script language=javascript></script> tags.
Syntax: required,optional
// inline comment
/* multiline comments */
Statements: statement; {statement;statement}
Variables: Place holders for values.
var var1,var2,var3; // declares var1, var2, and var3 as an untyped variable
var1 = expression; // assigns the value of an expression to var1
var str1 = new String(); //declares str1 as a string variable
str1 = "abcdef"; // assigns str1 the value abcdef
str1.big(); // returns <big>abcdef</big>
str1.concat("xxx"); //returns abcdefxxx
str1.charAt(1); // returns b
str1.charCodeAt(1); // returns 98, ascii for b
str1.fixed(); // returns <tt>abcdef</tt>
str1.indexOf("cd",0); // returns 2, c is the 2nd character
str1.indexOf("z",0); // returns -1
str1.length; // returns 6
str1.match("ab"); // returns 6
str1.replace("cd","xx"); // returns abxxef
str1.search("c") // returns 2
str1.search(/[c-z]||[a-c]/); // returns 2. /[c-z]/ is called a RegExp
str1.substr(2,2) // returns cd, substr(start,length)
str1.substring(2,3); // returns cd, substring(start,end)
str1.slice(2,3); // returns cd, substring(start,end)
arr1=str1.split("c");// arr1[0] returns ab, arr1[1] returns def
str1.toUpperCase() // returns ABCDEF
str1=str1 + 'hello' // str1=abcdefhello
str1+= 'hello' // str1 = abcdefhello
str1+= "\n" // str1 = abcdef + carriage return
var arr1 = new Array(value1,value2,value3...);
arr1[0] =value // assigns a value to an arr1[0]
arr1[1.2]=value // assigns a value to an arr2[1.2]
arr1['x']=value //assigns a value to arr3['x']
arr1.reverse()//reverses arr1
arr1.sort()//sorts arr1
Expressions: (expression) Returns a value. It can be a condition, an expression or constant.
nbr1=(nbr2 operator nbr3)... // operators are: *,/,+,-,%
nbr1=(8*3) // nbr1 = 24
nbr1=(8/3) // nbr1 = 2
nbr1=(8%3) // nbr1 = 2 (the remainder of 8/3)
nbr1=(8+3) // nbr1 = 11
nbr1=(8-3) // nbr1 = 5
nbr1++ // nbr1 = nbr1 + 1
nbr1+= 2 // nbr1 = nbr1 + 2
Conditions: (condition) // returns true or false, false is equal to 0.
condition is: (var1 test var2) // test is: ==,>=,<=,!=
test1=(8==3) // false, 8=3
test1=(8>=3) // false
test1=(8<=3) // true
test1=(8!=3) // true, ! means not
test1=!(8==3) // true, ! means not
test1=(8<3) // false
test1=(8>3) // true
test1=(8=3)||(8=8) // true, || means OR
test1=(8=3)&&(8=8) // false, && means AND
~ // um?l
?: // inline condition for if true
var date = new Date() //mydate is number of milliseconds since 1/1/1970, 00:00:00,
date // returns a date string in the format "Thu, 11 Jan 1996 06:20:00 GMT".
date.getDay(); // returns 0 = Sunday, 6=Saturday
date.getMonth() // returns 0 = January
date.getFullYear() // returns 1970 - 9999
date.getDate() // returns 1 = 1st, 31=31st
date.getHours() // returns 0 = 23
date.getMinutes() // returns 0-59
date.getSeconds() // returns 0-59
date.setDate(date2) // sets date to the date2 Date object
date.setFullYear('1999') // sets the year to 1999
date.setMonth(0) // sets the month to January
date.setHours(0) // sets the time to midnight
date.setMinutes(0)
date.setSeconds(0)
date.setTime(0) // ?????
date.toDateString()) // returns Fri May 6 2005
date.toLocaleDateString() // returns the date in your system format;
date.toString() // returns Fri May 6 01:00:00 PST 2005
var obj1 = new Object() // Creates a generic object
obj1.prop="hello" // Creates a property called prop for obj1 and sets it to hello
Statements
alert (value) // displays a popup with value and ok button
confirm (value) // displays a popup with value, and ok/cancel buttons. Returns true or false
for (var1=1;var1>=1;var=var+1) statement
{
break // exits the loop.
continue // executes next loop
}
for (property in object) statements // loops through object.*
function functionname (parameter,parameter) statements
{
return expression
}
if (condition) {statement} else {statement}
prompt (var1,var2) // displays a textbox showing var1, ok/cancel buttons. Returns the textbox value
setTimeout (functionname,value) // executes functionname after value. value=Seconds/1000.
while (condition) statements
with (object) property = value
Property references without an object use the default object
with (Math) {var xx=sin(angle); replaces var xx=Math.sin(angle);var x2=Math.cos(angle);
JavaScript Built-in Objects
Built//in objects can be accessed by both the client and server.
Math Provides numerical constants and mathematical functions.
Document // The text within the HTML's <BODY>>tag
document.write('text') // writes text to the current window
document.links('anchorName') // returns the href of <a href='www.yahoo.com' name='anchorName'>xxxx</a>
document.links('anchorID') // returns the href of <a href='www.yahoo.com' ID='anchorName'>xxxx</a>
form // An object for gathering and echoing data, created by HTML <FORM> tags.
window // is the Explorer's window
window.status = 'string'; // Sets the window status to 'string'
window.locaton = 'http://www.yahoo.com'; // Redirects the window to yahoo
window.print(); // prints the current window
window.blur(); // puts the current window behind the previous window
window.close(); //prompts to close the current window
window.defaultStatus = 'yes'; // sets the window default status
wdw=window.open (href,handle,features);
features are:,dependent=yes,fullscreen=yes,resizable=yes,
height=100px,width=100px,left=0px,top=0px,
status=yes,scrollbar=yes,toolbar=yes
popup=window.open() // creates a popup explorer window
popup.document.write("hello") // streams text to the popup
popup.document.write(" there") // streams more text
popup.window.focus() // brings the popup to the top
popup.window.close() // closes the popup