JavaScript DHTML/Language Basics/Variable Definition — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 10:24, 26 мая 2010

Event Handler with Multiple Statements in Attribute Value

   <source lang="html4strict">
 

<HTML> <HEAD> <TITLE>Event Handler With Multiple Statements</TITLE> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </HEAD> <BODY>

Event Handler With Multiple Statements

<A HREF="http://www.wbex.ru" ONMOUSEOVER="++count; alert("You moved your mouse here "+count+" times!")">Displays the number of times you move your mouse over this link.</A>

</BODY> </HTML>


 </source>
   
  


Get the type of a variable

   <source lang="html4strict">
 

<html> <head>

   <title>The Typeof Example</title>
   <script type = "text/javascript" >
       var aObject = {};
       aObject["A"] = new Object;
       aObject["A"].myValue = "a";
       document.write(typeof aObject["A"].myValue);
   </script>

</head> <body> </body> </html>


 </source>
   
  


Global and Local Variable Scope Demonstration

   <source lang="html4strict">
 

<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> var globalVar = "Boy" // global var globalVar2 = "dog" // global function demo() {

   var globalVar2 = "cat"    // local version of globalVar2
   var output = globalVar2 + " does not belong to " + globalVar + ".
" document.write(output)

} </SCRIPT> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"> demo(); document.write(globalVar2 + " belongs to " + globalVar + ".") </SCRIPT> </BODY> </HTML>



 </source>
   
  


Global scope and page scope

   <source lang="html4strict">
  

<html> <head> <title>Scope</title> <script type="text/javascript"> if (typeof(message) != "undefined")

 message += " globalPrint"; 

function globalPrint() {

 document.write(message);

} </script> <script type="text/javascript"> function global2Print() {

   message += " global2Print";
   document.write(message);

} </script> <script type="text/javascript"> message = "AAA"; function testScope() {

 message += " in testScope()";
 document.write(message);

} </script> </head> <body onload="testScope();global2Print();globalPrint()"> <script type="text/javascript"> message += " embedded in page"; document.writeln(message); </script> </body> </html>


 </source>
   
  


Global Versus Local Scope of a Variable

   <source lang="html4strict">
 

<html> <head>

 <title>Global Versus Local Scope of a Variable</title>
 <script type="text/javascript">
 
 </script>

</head> <body>

 <script type="text/javascript">
 
 </script>
 
<form name="outputForm"> <input name="changeButtonA" type="button" value="Change To B" onclick= "changeToB()"> <input name="changeButtonB" type="button" value="Change To C" onclick="changeToC()">

Value of globalString <p> <input name="beforeB" type="TEXT" size="5,1"> <p> Before clicking on "Change To B" <p> <input name="afterB" type="TEXT" size="5,1"> <p> After clicking on "Change To B" <p> <input name="beforeC" type="TEXT" size="5,1"> <p> Before clicking on "Change To C" <p> <input name="afterC" type="TEXT" size="5,1"> <p> After clicking on "Change To C" <p> </form> </body> </html> </source>

String value is passed by value, while the array is passed by reference

   <source lang="html4strict">
  

<html> <head> <title>Pass Me</title> </head> <body> <script type="text/javascript"> function alterArgs(strLiteral, aryObject) {

  strLiteral = "new value";
  aryObject[aryObject.length] = "three";

} var str = "old value"; var ary = new Array("one","two"); alterArgs(str,ary); document.writeln("string literal is " + str + "
"); document.writeln("Array object is " + ary); </script> </body> </html>


 </source>
   
  


The Effects of Local and Global Variables

   <source lang="html4strict">
 

<html> <head>

 <title>JavaScript Unleashed</title>
 <script type="text/javascript">
 
 </script>

</head> <body>

 <script type="text/javascript">  
 </script>

</body> </html>


 </source>
   
  


Use of Global and Local Variables

   <source lang="html4strict">
 

<HTML> <HEAD> <TITLE>Global and Local Variables</TITLE> <SCRIPT LANGUAGE="JavaScript"> </SCRIPT> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"> </SCRIPT> </BODY> </HTML>


 </source>
   
  


Variable scope

   <source lang="html4strict">
 

<html> <head> <script language="JavaScript">

</script> </head> </html>



 </source>
   
  


Variable scoping

   <source lang="html4strict">
 

<html> <head>

   <title>Scoping Example</title>
   <script type = "text/javascript" >
   var aNewVariable = "global.";
   function myFunction(myPara) {
       document.write("Global variable within the function: " + aNewVariable);
       document.write("Local variable within the function: " + myPara);
   }
   </script>

</head> <body> <script type = "text/javascript" >

   myFunction("local");
   document.write("Global var outside the function: " + aNewVariable);
   document.write("Local var outside the function: " + myPara);
   

</script> </body> </html>


</source>