Popup Window: Don't Reload

In most situations the page in the popup should reload even if the popup was already open. In this page we'll look at a script in which the page in the popup does not reload if the window is already open. One popular use for this script is when the popup contains a piece of music that should continue uninterrupted as the user moves through the site.

We'll begin by modifying the basic popup script from our first example. The following script is similar to the first example, but with one important difference: before directing the popup to a URL the script first checks if the popup is already at that URL. If the popup is already at the URL then no action is taken:

<SCRIPT TYPE="text/javascript">
function popupnr(mylink, windowname, refocus)
var mywin, href;
if (typeof(mylink) == 'string')
mywin = window.open('', windowname, 'width=400,height=200,scrollbars=yes');
// if we just opened the window
if (
mywin.closed || 
(! mywin.document.URL) || 
(mywin.document.URL.indexOf("about") == 0)
else if (refocus)
return false;

Use this script in the same way as in our first example. Call popupnr() in the onClick attribute. The first argument is this, meaning the link object itself, and the second argument is a unique name for the popup. For example, this link opens a popup that plays some background music. The music keeps playing uninterrupted even when you click on the link several times:

onClick="return popupnr(this, 'music')">music</A>

which gives us this link:


Try clicking on the link and then, without closing the popup, come back to this page and click on the link again. On the second click nothing changes: the popup stays in the back and the music keeps playing uninterrupted.

External URLs

Do not use this script if the page in the popup has a URL that is external to our site. For example, if your site is in the domain www.idocs.com, don't attempt to load a popup with a page from www.ninthwonder.com. For security reasons JavaScript cannot read the URL information for sites outside the domain of the current web site.


Generally a popup that doesn't reload should also not refocus (i.e. come back to the front). If you do want the popup to come to the front add true as a third argument to popupnr():

onClick="return popupnr(this, 'music',true)">music</A>

which gives us this link:


You can also use this script to open popups automatically. Call popupnr() in the onLoad attribute in the same manner as with regular Popup Windows: Opening Automaticallyautomatic popups. The first argument is a unique name for the popup, the second argument is the URL of the popup:

this code produces this
<BODY onLoad="return popupnr('nrl.html', 'music')">
this page

About the Author
Copyright 1997-2002 Idocs Inc. Content in this guide is offered freely to the public under the terms of the Open Content License and the Open Publication License. Contents may be redistributed or republished freely under these terms so long as credit to the original creator and contributors is maintained.