Global scope and page scope : Variable Definition « Language Basics « JavaScript DHTML






Global scope and page scope

   

<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>

   
    
    
  








Related examples in the same category

1.The Effects of Local and Global Variables
2.Use of Global and Local Variables
3.Event Handler with Multiple Statements in Attribute Value
4.Global and Local Variable Scope Demonstration
5.Global Versus Local Scope of a Variable
6.Variable scope
7.Variable scoping
8.Get the type of a variable
9.String value is passed by value, while the array is passed by reference