Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- Problem with JavaScript popup windows


rewboss - 11:49 am on Jul 21, 2002 (gmt 0)


You have to be quite clear about the difference between a name and a reference to an object.

When you use JavaScript to create a window using window.open(), the function returns a window object. If you want to manipulate the window using JavaScript, you will need to store the object in a variable. The name of the window is one of the properties of the Window object.

When you type:

var myWindow=window.open('about:blank','popup','width=300,height=400');

that returns a Window object which is referenced by the variable myWindow. That object contains all the properties and methods you need to manipulate that window. For example:

myWindow.document.location='newpage.html';
myWindow.resizeTo(200,200);
myWindow.document.write('<h1>Hello world</h1>');
myWindow.close();
if(!myWindow.closed) valueForHTMLtargetAttribute=myWindow.name;

One of the many properties available in the Window object referenced by the variable myWindow is myWindow.name.

This is a String, not a Window. So you can use String methods and access String properties, but you can't use Window methods or properties on it. This is OK:

myWindow.name.toUpperCase();
var lengthOfName=myWindow.name.length;

This is not:

myWindow.name.close();

If you try this:

window.open('','popup','');

it will open a window, and you will be able to target <a> tags using <a target="popup" ...>, but you won't be able to manipulate it with JavaScript because you cannot reference the Window object -- you have no access through JavaScript of the name of the new window, which is a string stored in the name property of the new Window object. But you have not stored a reference to the object anywhere: it exists, but you can't use it.

popup will not have been initialized as a variable; if you try

popup.document.write('<h1>Hello World</h1>');

you will get an error message to the effect that "popup.document has no properties". What this means is that the top-level variable popup does not contain an object called document -- not surprising, because it has only just been initialized and nothing has been assigned to it.

When working with client-side JavaScript, you need to fully understand what objects are and how windows and frames are referenced from each other. It can get very confusing -- as here -- which is why it is important also to learn the terminology.


Thread source:: http://www.webmasterworld.com/javascript/158.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com