18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5<br />

As mentioned previously, the feature string is comma-delimited and, therefore, must contain no space<br />

before or after a comma or equal sign. <strong>For</strong> example, the following string is invalid:<br />

window.open(“http://www.wrox.com/”, “wroxwindow”,<br />

“height=150, width= 300, top=10, left= 10, resizable =yes”);<br />

This string won’t work because of the spaces after the commas and other spaces around a couple of<br />

equal signs. Just remove the spaces and it works fine:<br />

window.open(“http://www.wrox.com/”, “wroxwindow”,<br />

“height=150,width=300,top=10,left=10,resizable=yes”);<br />

The window.open() method returns a window object as its function value that is also the window object<br />

for the newly created window (or for the frame, if the name given is the name of an existing frame).<br />

Using this object, it’s possible to manipulate the new window:<br />

var oNewWin = window.open(“http://www.wrox.com/”, “wroxwindow”,<br />

“height=150,width=300,top=10,left=10,resizable=yes”);<br />

oNewWin.moveTo(100, 100);<br />

oNewWin.resizeTo(200, 200);<br />

Also using this object, it is possible to close the new window using the close() method:<br />

oNewWin.close();<br />

If there is code in the new window, it can close itself by using:<br />

window.close();<br />

This only works in the new window. If you try to call window.close() in the main browser window,<br />

you get a message saying that a script is trying to close the window and asking if you actually want it to<br />

close. The general rule to remember is this: Scripts can close any windows that they open, but no others.<br />

A new window also has a reference to the window that opened it stored in the opener property. The<br />

opener property exists only on the topmost window object of the new window, making it safer to use<br />

top.opener to access it. Example:<br />

var oNewWin = window.open(“http://www.wrox.com/”, “wroxwindow”,<br />

“height=150,width=300,top=10,left=10,resizable=yes”);<br />

alert(oNewWin.opener == window);<br />

//outputs “true”<br />

In this example, a new window is opened and then its opener property is tested against the window<br />

object to prove that opener does indeed point to window (this alert displays “true”).<br />

142

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!