Tutorial ******** How To... ========= The HTML `` In general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line. Browsers that do not support JavaScript will display the script as page content. To prevent them from doing this, we may use the HTML comment tag: Where To... =========== http://www.w3schools.com/js/js_whereto.asp Scripts to be executed when they are called, or when an event is triggered, go in the head section. When you place a script in the head section, you will ensure that the script is loaded before anyone uses it. Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page. :: Using an External JavaScript ---------------------------- Sometimes you might want to run the same JavaScript on several pages, without having to write the same script on every page. To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file with a .js file extension. Note: The external script cannot contain the Note: Remember to place the script exactly where you normally would write the script! Variables ========= http://www.w3schools.com/js/js_variables.asp Rules for variable names: - Variable names are case sensitive - They must begin with a letter or the underscore character You can assign a value to a variable like this: :: var strname = "Hege" Or like this: :: strname = "Hege" ...the ``var`` keyword is used to declare local (as opposed to global) variables. When you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared. If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed. If...Else Statements ==================== http://www.w3schools.com/js/js_if_else.asp :: Switch Statement ================ :: Operators ========= http://www.w3schools.com/js/js_operators.asp Popup Boxes =========== http://www.w3schools.com/js/js_popup.asp Alert ----- When an alert box pops up, the user will have to click "OK" to proceed. :: alert("sometext") Confirm ------- When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. :: confirm("sometext") Prompt ------ When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. :: prompt("sometext","defaultvalue") Functions ========= http://www.w3schools.com/js/js_functions.asp You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file). Functions are defined at the beginning of a page, in the section. ::
If the line: ``alert("Hello world!!")``, in the example above had not been written within a function, it would have been executed as soon as the line was loaded. Now, the script is not executed before the user hits the button. We have added an onClick event to the button that will execute the function displaymessage() when the button is clicked. The return Statement -------------------- The return statement is used to specify the value that is returned from the function. So, functions that are going to return a value must use the return statement. :: function prod(a,b) { x = a * b return x } When you call the function above, you must pass along two parameters: :: product = prod(2, 3) JavaScript does not support passing by reference. It passes its parameters by value only, http://www.webreference.com/js/tips/010210.html. For Loop ======== http://www.w3schools.com/js/js_loop_for.asp :: While Loop ========== http://www.w3schools.com/js/js_loop_while.asp while ----- :: do... while ----------- :: Break and Continue ================== http://www.w3schools.com/js/js_break.asp The break command will break the loop and continue executing the code that follows after the loop (if any). The continue command will break the current loop and continue with the next value. For...In Statement ================== http://www.w3schools.com/js/js_loop_for_in.asp The for...in statement is used to loop (iterate) through the elements of an array or through the properties of an object. :: Events ====== http://www.w3schools.com/js/js_events.asp By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript. Every element on a web page has certain events which can trigger JavaScript functions. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags. Examples of events: - A mouse click - A web page or an image loading - Mousing over a hot spot on the web page - Selecting an input box in an HTML form - Submitting an HTML form - A keystroke Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs! For a complete reference of the events recognized by JavaScript, go to http://www.w3schools.com/jsref/jsref_events.asp. Try...Catch Statement ===================== http://www.w3schools.com/js/js_try_catch.asp The example below contains a script that is supposed to display the message "Welcome guest!" when you click on a button. However, there's a typo in the message() function. alert() is misspelled as adddlert(). A JavaScript error occurs: :: Throw Statement =============== http://www.w3schools.com/js/js_throw.asp :: onerror ======= Using the onerror event is the **old** standard solution to catch errors in a web page. http://www.w3schools.com/js/js_onerror.asp Special Characters ================== http://www.w3schools.com/js/js_special_characters.asp In JavaScript you can add special characters to a text string by using the backslash sign.