Chapter 9. Windows and Frames


Windows are playing an important role in JavaScript. The window object is the default object in JavaScript and is used quite often (just consider the windows.alert() method). Also, JavaScript is capable of working with windows (for instance, by opening new ones)and also with frames and iframes, which are also very similar to window objects.

window.open("http://www.samspublishing.com/",   "samsWindow", ""); 

The method window.open() opens a new window and expects at least three parameters so that the method call is useful: the URL to open, the new name of the window, and window options.

With the preceding code (file open.html), a new window is opened with the publisher's homepage opened.

The second parameter of the open() method provides the name of the new window. This works exactly as a frame name: If you use the window name as the target attribute for a link, the link opens in the new window.

Therefore, the open.html sample file also contains a link that is using the new window name:

[View full width]

<a href="http://www.amazon.com/gp/product/0672328801" target="samsWindow">This book at Amazon</a>


When you click on that link, no new window is opened but the old window, previously opened by JavaScript, is reused.

Note that the return value of window.open() is a JavaScript window object pointing to the new window. So code in the following fashion does not lead to the desired result:

<a href="javascript:window.open(...)">Open window</a> 


Since the method returns a window object, this link would write something like [object] (Internet Explorer) or [object Window] (Mozilla browsers) to the page. Therefore, you must make sure that the JavaScript code does not return anything. A good way to achieve this is via the following code:

<a href="javascript:void(window.open(...))">Open window</a> 





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net