Useful Javascript Codes

Contents
1) Executing Application from Javascript
2) Disable Back Button in IE
3) JavaScript - Image Change Using Functions
4) JavaScript Event Handlers - onmouseover, onmouseout, Onclick and OnDblClick
5) Creating or Opening New Windows (pop-up) in JavaScript
6) JavaScript Errors - Understanding and Correcting
7) Browser detection

----------------------------------------------------------------------------
1) Executing Application from Javascript
script language="javascript"
function run()
{
var WshShell = new ActiveXObject("WScript.Shell")
WshShell.Run("notepad.exe")
}
/script


----------------------------------------------------------------------------

3) JavaScript - Image Change Using Functions
function roll_over(img_name, img_src)
{
document[img_name].src = img_src;
}
We can now call this function from our even handlers and pass it the two arguments.
----------------------------------------------------------------------------

4) JavaScript Event Handlers – onmouseover, onmouseout, onclick, ondblclick

On Mouse Over
Ex 1: Change the background color and put a message on the status bar

Ex 2: Change the background color and bring up an alert box Ex 3: Changes the background color when the mouse is placed and then taken off the link

Ex 4: Move your mouse over me! Ex 5: Dead link Ex 6: Double click on me

----------------------------------------------------------------------------
5) Creating or Opening New Windows (pop-up) in JavaScript
Ex 1: Open a new window Syntax: window.open('URL', 'NAME', 'FEATURES', 'REPLACE') Ex 2: Open a new window Ex 3: Open a new window Ex 4: Open a full screen window
----------------------------------------------------------------------------

6) JavaScript Errors - Understanding and Correcting
JavaScript beginners mainly make three kinds of errors
· They might try to call a variable that is not yet defined.Solution: Remember to always initialize all variables you expect to use in the script.
· Forgetting the starting or ending quote or parenthesis as in a method or function.Solution: Place the opening and ending parenthesis or quotes together, then write the code/text between them.
· Using a property or method that does not belong to an object, for example, using document.alert() instead of window.alert().Solution: You don't have to learn each and every object's properties and methods at the very start... you'll get the hang of it all gradually.
----------------------------------------------------------------------------

7) Browser detection through JavaScript - Navigator Object
Getting client (browser) details is very easy through JavaScript. Client name, version, codename and the platform used are available through the navigator object and its properties. (The navigator object was named after Netscape Navigator).
· navigator.appName - Gives the name of the browser (application Name)
· navigator.appVersion - Gives the browser version (application Version)
· navigator.appCodeName - Gives the browser codename (application CodeName)
· navigator.platform - Gives the platform on which the browser is running


Deconstruction of this scriptThe browser name is obtained through navigator.appName and is stored in variable bname. Using an if statement we check the value of this variable.

If it's "Microsoft Internet Explorer", we transfer the visitor to explorer_version.html else, the visitor is taken to netscape_version.html. Note that window.location takes a URL as value.

You May Also Like

0 comments