Forum Moderators: open

Message Too Old, No Replies

Check if window is open if so do not load the url

         

andre73

10:28 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Hi, I wonder if someone can help with the following: Links on several different pages point to the same javascript function which opens a window, loads a document and brings the window to the front. The problem with this is that if the user has opened a window and loaded a document in it (the documents loaded in the pop-ups contain forms) and then clicks the link again the document gets reloaded and the data already entered into the form gets lost. I need the function to check if the window is already open and if it is the document should not be reloaded, just brought to the front. If the window does not exist the function needs to create the window, load the desired document and bring it to the front. Hope someone has a solution to this.

The code I use today is pasted below.

<script language="javascript">
var newwindow;
function popUp(url, w, h, namn)
{
newwindow=window.open(url, namn, 'width='+ w +', height=' + h + ' resizable=no, scrollbars=auto, toolbars=no, location=no, directories=no, status=no, menubar=no, copyhistory=no');
if (window.focus) {newwindow.focus()}
}
</script>
<body>
<p><a href="javascript:popUp( 'http://www.svt.se', 200, 200, 'little' )">open little pop-up</a></p>
<p><a href="javascript:popUp( 'http://www.abb.se', 500, 500, 'bigger')">open bigger pop-up</a></p>
<p><a href="javascript:popUp( 'http://www.svt.se', 200, 200, 'little' )">open little pop-up</a></p>
<p><a href="javascript:popUp( 'http://www.abb.se', 500, 500, 'bigger')">open bigger pop-up</a></p>
</body>

Rambo Tribble

2:28 am on Oct 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you eliminate the initialization of your variable newwindow at the top of your script with "var newwindow", it will not exist until the window exists. Then, simply testing for the variable's existence will allow you to branch your code. Consider:

<a href="#" onclick="typeof var1=='undefined'? var1='' : alert('');return false;">test for var</a>