Working with Sub Window Properties

Loading Multiple Documents into the Same Pop Up Window

When you want to use one generic function to open multiple documents into the same pop-up window, replace the open method's file name parameter with an argument. The argument, which will be the file name, is defined when you call the function. Every time the function is called, a different file name can be passed back, hense loading a different file into the window for each function call. Because the new windows have the same name (win1), only one new window is created. Now one function can be used to open any number of pages in your new window.
function popUp(URL){

pop = window.open(URL,"win1","width=400,height=500,resizable=1,scrollbars=1")

pop.focus()

}

Changing Window Properties on the fly

If you need to change the properties (width,scrollbars,top, etc.) of a window for each new document, the function gets slightly more complex. NN 4.x has no problem allowing you to change properties on the fly, but MSIE will not allow you to change window properties. Because of this, your function needs to close the pop up window (if it is open) first, then re-open it with its changed properties.

Try clicking on the following links consecutively without closing the window they launch.

width=100,height=100,location=0,status=0

width=500,height=500,location=1,status=0

width=500,height=250,location=1,status=1

When you need flexibility in terms of changing window properties with each call to the function, add the following lines:
var pop //create global variable without assigning a value so that its value is null

function popUp(URL,w,h){

if(pop!=null && !pop.closed){ // if popUp has been opened, and popUp hasn't already been closed
pop.close() //close it in order to re-assign properties
}

pop = window.open(URL,"win1","width="+w+",height="+h+",resizable=1,scrollbars=1")

pop.focus()
}

Checking For a Value of Null

First, the variable "pop" is initialized as a global variable (outside a function body) and not assigned a value - which essentially leaves it with a value of null. When the function is invoked, pop is given a value just by assigning the open method to it, so its value is no longer null. If the function has been invoked ("pop" is NOT equal to null), the script continues on to the next check. Otherwise, the if statement is "short-circuited" and no further check is performed.

Checking the Closed Property

The second evaluation performed by the if statement checks the window's closed property. If the closed property is true, the window has been closed. If it evaluates to false, the window has not been closed. If the window has not yet been closed (!pop.closed evaluates to true), it is closed so that it may be re-assigned new properties when it is opened again.
Check the status of the variable, "pop" once before the function has executed, then after.

The Google Toolbar and Popup Window Script Error

Recently I've encountered a huge amount of script error notifications (using Internet Explorer's script notification debugger tool) while surfing. It has to do with popup windows which are opened automatically via a script - you know, the kind of popups we all hate - and the Google Toolbar Popup Blocker (possibly other popup blockers too).

The fix is extremely simple and has everything to do with null. I'm not including the fix in all my popup window code samples because it doesn't apply to all popup scripts, just those which automatically launch a popup. If the above function were used in this evil way, the fix would read as follows:

[FIND]
pop = window.open(URL,"win1","width="+w+",height="+h+",resizable=1,scrollbars=1")
[REPLACE WITH]
if(pop != null) pop = window.open(URL,"win1","width="+w+",height="+h+",resizable=1,scrollbars=1")

View the Source


 


View Source | Home | Contact