JavaScript

System Dialogs
Alert Dialogs

Alert dialogs are typically used when users must be made aware of something that they have no control over, such as errors. Often alert dialogs are displayed when the user has entered invalid data into a form. Example:

alert(“Hello JavaScript!“);

Confirm Dialogs

The second type of dialog is displayed by calling. A confirm dialog looks similar to an alert dialog in that it displays a message to the user. The main difference between the two is the presence of a Cancel button along with the OK button in the confirm dialog, which allows the user to indicate if a given action should be taken. Example:

if (confirm(“Are you sure? “)) {	alert(“I’m so glad you’re sure! “); } else { alert(“I’m sorry to hear you’re not sure. “); }

Prompt Dialogs

The final dialog is displayed by calling, and as you might expect, this dialog prompts for input from the user. Along with OK and Cancel buttons, this dialog also has a text box where the user is asked to enter some data.

var sResult = prompt(“What is your name? “, “”);

if (sResult != null) { alert(“Welcome, “ + sResult); }

Changing the Status Bar value
you may use a default status bar message when the pag first loads:

window.defaultStatus = “You are surfing www.wrox.com. “;

You may also want to display information about a certain link when the user moves the mouse over it:

Books

Navigation with History
Use the following code to navigate backward or forward:

window.history.go(-1); Back to the previous page //go back one history.back; //go forward one history.forward;

Adding load event
Use the following solution to add a load event into your web page:

function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function { oldonload; func; }		}	}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad); addLoadEvent(function {		/* more code to run on page load */	});

Using External JavaScript files

 * External JavaScript files should have a .js extension,
 * It is common to place all code and function definitions in the tag of an HTML page so that the code is fully loaded and ready for use once the body is rendered.

Title of Page  var i = 0; 

Use External JavaScript files for many reasons like: Security, Code Maintenance and Performance / Caching

Navigating and opening new windows
If window.open is called with the name of an existing frame as the second argument, the URL is then loaded into the frame with that name. For example, to load a page into the frame named “topFrame”, the following code does the trick:

window.open(“http://www.wrox.com/”, “topFrame”);

window.open(“http://www.wrox.com/”, “MyWindow”,  “height=150, width= 300, top=10, left= 10, resizable =yes”);

This line of code behaves as if a user clicked a link with the href set to http://www.wrox.com/ and the target set to “topFrame”. The special frame names _self, _parent, _top, and _blank are also valid.