JavaScripts in the body section will be executed WHILE the page loads. JavaScripts in the head section will be executed when CALLED. * Variable names are case sensitive * They must begin with a letter or the underscore character var strname = some value if (condition) { code to be executed if condition is true } switch(n) { case 1: execute code block 1 break case 2: execute code block 2 break default: code to be executed if n is different from case 1 and 2 } txt1="What a very" txt2="nice day!" txt3=txt1+txt2 greeting=(visitor=="PRES")?"Dear President ":"Dear " alert("sometext") confirm("sometext") <-- oK or cancel prompt("sometext","defaultvalue")
function prod(a,b) { x=a*b return x } for (var=startvalue;var<=endvalue;var=var+increment) { code to be executed } events: http://www.w3schools.com/jsref/jsref_events.asp onabort Loading of an image is interrupted 1 3 4 onblur An element loses focus 1 2 3 onchange The content of a field changes 1 2 3 onclick Mouse clicks an object 1 2 3 ondblclick Mouse double-clicks an object 1 4 4 onerror An error occurs when loading a document or an image 1 3 4 onfocus An element gets focus 1 2 3 onkeydown A keyboard key is pressed 1 4 3 onkeypress A keyboard key is pressed or held down 1 4 3 onkeyup A keyboard key is released 1 4 3 onload A page or an image is finished loading 1 2 3 onmousedown A mouse button is pressed 1 4 4 onmousemove The mouse is moved 1 6 3 onmouseout The mouse is moved off an element 1 4 4 onmouseover The mouse is moved over an element 1 2 3 onmouseup A mouse button is released 1 4 4 onreset The reset button is clicked 1 3 4 onresize A window or frame is resized 1 4 4 onselect Text is selected 1 2 3 onsubmit The submit button is clicked 1 2 3 onunload The user exits the page 1 2 3 try { //Run some code here } catch(err) { //Handle errors here } throw(exception) The exception can be a string, integer, Boolean or an object. The onerror event is fired whenever there is a script error in the page. To use the onerror event, you must create a function to handle the errors. Then you call the function with the onerror event handler. The event handler is called with three arguments: msg (error message), url (the url of the page that caused the error) and line (the line where the error occurred). onerror=handleErr function handleErr(msg,url,l) { //Handle the error here return true or false } var txt="We are the so-called \"Vikings\" from the north." document.write(txt) document.write ("You \& me are singing!") \' single quote \" double quote \& ampersand \\ backslash \n new line \r carriage return \t tab \b backspace \f form feed document.write("Hello \ World!") //this is a comment document.write("Hello World!") /* This is a comment block. It contains several lines */ document.write("Hello World!") The following code adds a method called eat() to the personObj: personObj.eat=eat To add some methods to the person object inside the template: function person(firstname,lastname,age,eyecolor) { this.firstname=firstname this.lastname=lastname this.age=age this.eyecolor=eyecolor this.newlastname=newlastname } Methods are just functions attached to objects. You have to write the newlastname() function: function newlastname(new_lastname) { this.lastname=new_lastname }