The most commonly used object within the Browser Object Model and which all browser support is window which is the object that represents the browser window. Almost everything that you have in the browser is a property or method of that object. Even the Document Object Model for the web page is attached as window.document.
It is so common that what you need will be attached to the window object that JavaScript allows you to omit it from the front of the reference in many cases and so a lot of people do not realise that it is there.
There are a couple of different ways of referencing the properties and methods of the window object though and one where the reference to window on the front isn't optional can be extremely useful. It is worth remembering that the windows object exists.
You can of course add your own properties and methods to the windows object at any time. The following are three ways of defining a property called myProperty belonging to the window object.
You can then access the property from anywhere within your page using the same reference (without the var for the first one). The third of these ways of referencing the property is particularly useful when it comes to being able to reference properties where part or all of the property name is defined by another property.
If you have a number of properties named x1, x2, x3 etc. and you have another property that contains the number off the end of the x property that you want to access you can access it using window['x'+y] which accesses the property directly without the overhead that using eval('x'+y) has.
This is one of the reasons why you should avoid using eval() in your code - since it is just an extremely inefficient way of achieving the same result as can be achieved far more efficiently another way.
Methods also attach to the windows object unless you specify an alternative object that you want to attach them to.
It is so common that what you need will be attached to the window object that JavaScript allows you to omit it from the front of the reference in many cases and so a lot of people do not realise that it is there.
There are a couple of different ways of referencing the properties and methods of the window object though and one where the reference to window on the front isn't optional can be extremely useful. It is worth remembering that the windows object exists.
You can of course add your own properties and methods to the windows object at any time. The following are three ways of defining a property called myProperty belonging to the window object.
var myProperty; // assuming this is not inside a functionwindow.myProperty;window['myProperty'];
You can then access the property from anywhere within your page using the same reference (without the var for the first one). The third of these ways of referencing the property is particularly useful when it comes to being able to reference properties where part or all of the property name is defined by another property.
If you have a number of properties named x1, x2, x3 etc. and you have another property that contains the number off the end of the x property that you want to access you can access it using window['x'+y] which accesses the property directly without the overhead that using eval('x'+y) has.
This is one of the reasons why you should avoid using eval() in your code - since it is just an extremely inefficient way of achieving the same result as can be achieved far more efficiently another way.
Methods also attach to the windows object unless you specify an alternative object that you want to attach them to.
More of this Tutorial
SHARE