Previous Example-|-Next Example-|-Return to Chapter Listing

Example 4.6:
Why You Should Use Var--Fixed Example


Now we've added the var keyword in the Display2 function, the buttons work correctly.



This is the script we used. First we created the variables and functions in the HEAD <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- var func1 = "This is the global variable named func1" function Display1() { alert(func1) } function Display2() { var func1 = "This is local variable named func1" alert(func1) } //--> </SCRIPT> Then we created two buttons: <FORM> <INPUT TYPE="button" NAME="ButtonDisplay1" VALUE="Display1" onclick=Display1()><BR> <INPUT TYPE="button" NAME="ButtonDisplay2" VALUE="Display2" onclick=Display2()> <FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing