When you use the JavaScript window.open method to open a new browser window you may experience problems when you try to re-use it.
For example, open the link below and then click back inside this window so that the new one goes behind it.
window.open('/icons/vwicn001.gif', 'SampleWindow1', 'WIDTH=300,HEIGHT=250');
This has opened a new instance of the browser called 'newWindow'.
Now click on the above link again. The link does not open a new window as expected. That is because a window with that name ('newWindow') already exists. The link is actually working, you just can't see it.
This method is fine if your users are wise enough to know that they need to toggle back to the new window using the "task-bar" or similar method. But, most users will end up confused and annoyed after clicking the link a dozen times expecting it to appeat in front of them.
The solution is to use the following customised function in the place of the window.open method that checks to see whether or not a window of that name is already open. If so, then it will call focus on it so that it is returned to the foreground.
Note: It is a good idea to make this function available globally and to then always use it in place of the window.open method (it accepts exactly the same arguments).
function windowOpener(url, name, args) {
if (typeof(popupWin) != "object"){
popupWin = window.open(url,name,args);
} else {
if (!popupWin.closed){
popupWin.location.href = url;
} else {
popupWin = window.open(url, name,args);
}
}
popupWin.focus();
}
Proof of the pudding:
Clink the link below and then return to this window.
windowOpener('/icons/vwicn002.gif', 'SampleWindow2', 'WIDTH=300,HEIGHT=250');
Now click the above link again. It should come back in to view.....
What is the above function doing?
The first thing it does is to see if it has already been run by checking for a variable called 'popupWin'. If it is the first time then this variable is set to an object variable by opening a new window. If the window still exists then its attributes are reset else a new window is opened. The final thing to do is focus on said window.
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net