My favorites | Sign in
Project Logo
             
Search
for
Updated Sep 11, 2008 by pilgrim
Labels: about-dom, is-dom-object
WindowObject  
The window object represents the current browser window.

It is also the "global" object in JavaScript. As such, any global variable that you define is accessible as a property of the window object. This can avoid unnecessary use of eval() to retrieve a global object/function for which the name is only available in the form a string.

E.g.

<script type="text/javascript">
var myVariable = 23;

function aFunction(paramA) {
  alert(myVariable);           // 23
  alert(window.myVariable);    // 23
  alert(window['myVariable'];  // 23

  if (typeof paramA === 'string') {
    alert(window[paramA]);    // 23 if paramA is 'myVariable'
  }

  alert(eval('myVariable'));   // 23, but much slower because the compiler has to kick in.
  
  if (typeof paramA === 'string') {
    alert(eval(paramA));      // 23 if paramA is 'myVariable' but again slower, and poses security risk.
  }
}
</script>

EspaƱol

Properties

Methods

Further reading


Sign in to add a comment
Hosted by Google Code