HTML 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')
 href=mylink;
else
 href=mylink.href;
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)
 )
 mywin.location=href;
else if (refocus)
 mywin.focus();
return false;
}
//-->
</SCRIPT>

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:

<A 
 HREF="nrl.html" 
onClick="return popupnr(this, 'music')">music</A>

which gives us this link:

music

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.google.com, don't attempt to load a popup with a page from www.Link-Popularity-Checker.com. For security reasons JavaScript cannot read the URL information for sites outside the domain of the current web site.

Refocus

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():

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

which gives us this link:

music

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 Automatically automatic popups. The first argument is a unique name for the popup, the second argument is the URL of the popup:

This Code Makes This
<BODY onLoad="return popupnr('nrl.html', 'music')">
this page

Popup Windows: open() Parameters >