Raymond.Pittman
08-12-2007, 05:28 AM
Popup Windows: The Basics
We'll begin the tutorial by creating a basic popup window. The technique described here addresses all the major issues in popups. The popup always comes to the front. Different links can target the same popup. The code is simple and easily modified. Everything for the rest of the turorial is a variation on the theme described here. The code in this page creates a popup that is opened from a link. In this section we'll show the code with just the minimal description you need to get it going.
First, copy this script into the <HEAD> section of your page:
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}
//-->
</SCRIPT>
For now we'll skip the details of how the script works, and move to the next step. The script above opens the popup, but something needs to run the script. The most common situation is that the script is run when the user clicks on a link. A link like the following would run the script:
<A
HREF="popupbasic.html"
onClick="return popup(this, 'notes')">my popup</A>
Most of the link is as usual. The URL of the page being linked to is in the HREF attribute. We've added an additional attribute called onClick. Copy the code as it is into your link, with only a small modification. The second argument of the popup() -- 'notes' -- indicates name of the popup window. Every popup window should have its own unique name. Be sure to put the name in single quotes (''). So if you want to name the popup 'stevie' then this would be the code:
<A HREF="popupbasic.html" onClick="return popup(this, 'stevie')">my popup</A>
Read This Next Part Or You'll Go Insane Trying to Figure Out Why Your Popup Doesn't Work
A small but crucial point is often overlooked. The command in onClick must begin with return or the script won't work. Be sure to start the command with return like this:
onClick="return popup(this, 'notes')"
And don't put a space in the page name between the single quotes. If you do, the link will act just like a regular link.
Popup Windows: From an Image Map
In our first variation we'll open the popup from an image map instead of from a regular anchor. We'll use the same script as from our first example. With that script, an <AREA ...> tag in an image map can be made to open a popup in exactly the same way as an <A ...> tag:
<MAP NAME="index">
<AREA
HREF="mypopup.html" ALT="My Popup"
COORDS="10,10,120,120" SHAPE=RECT
onClick="return popup(this, 'gloss')">
<AREA
SHAPE=RECT ALT="Your Popup"
COORDS="140,10,180,50" HREF="yourpopup.html"
onClick="return popup(this, 'gloss')">
<AREA SHAPE=DEFAULT NOHREF>
</MAP>
<IMG SRC="mymap.gif" HEIGHT=130 WIDTH=190 ALT="Image Map Example"
BORDER="0" USEMAP="#index">
Popup Windows: Opening Automatically
In the first two examples (Popup Windows: The Basics and Popup Windows: From an Image Map) the popup is opened when the user clicks on something. In this example the popup opens automatically.
We'll use the same script as in the first example. Copy this script in the <HEAD> section of your page:
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}
//-->
</SCRIPT>
This time, instead of running the script from a link we'll run it from the onLoad attribute of the <BODY ...> tag.
<BODY onLoad="popup('autopopup.html', 'ad')">
The command in onLoad is run when the document is finished loading. Like in our previous example, the command runs popup(), but this time the first argument for popup() is a little different. In the previous example we put this, meaning the link itself, and the script got the URL from the link. In this case there is no link so we pass the actual URL to open. So in our example we put 'autopopup.html'.
Popup Windows: From a Form
For our next variation on the popup theme we're going to open the popup from a form. In this example we're going to change the script around. The following script is custom designed for opening a popup from a form. It works with forms that use both POST and GET. Copy the following script exactly as-is into the <HEAD> section of your document:
<SCRIPT TYPE="text/javascript">
<!--
function popupform(myform, windowname)
{
if (! window.focus)return true;
window.open('', windowname, 'height=200,width=400,scrollbars=yes');
myform.target=windowname;
return true;
}
//-->
</SCRIPT>
Now we'll add some code so that the popup opens when the user submits the form. Add an onSubmit attribute to <FORM ...> tag:
<FORM METHOD=POST
ACTION="../cgi-bin/mypopupcgi.pl"
onSubmit="popupform(this, 'join')">
The first argument for popupform() is always this, meaning the form itself. The second argument, 'join' in this case, is a unique name for the popup.
Popup Windows: Targeting the Opener
Once a popup window has been created, linking from the popup back to the main window (i.e. the window which opened the popup) is a little trickier than might be expected. The problem is that the main window doesn't have a "name" the way the popup window does. Fortunately, JavaScript provides an answer in the form of opener.
To create links in the popup window that target back to the main window, first put this JavaScript in the <HEAD> of the popup page:
<SCRIPT TYPE="text/javascript">
<!--
function targetopener(mylink, closeme, closeonly)
{
if (! (window.focus && window.opener))return true;
window.opener.focus();
if (! closeonly)window.opener.location.href=mylink.href;
if (closeme)window.close();
return false;
}
//-->
</SCRIPT>
A link that uses this script looks like this:
<A
HREF="rbex.html"
onClick="return targetopener(this)">my page</A>
Popup Windows: Closing When They Go to the Opener
In the previous example the link in the popup targets the main page, but the popup stays open in the background after the user clicks on the link. In this page we'll set the link so that it closes the popup after the click.
targetopener takes three parameters. The first is always this, meaning the link itself. The second and third parameters are optional and default to false. (Notice we don't use them in the example above, we'll get to them shortly.) The second parameter indicates if the popup should close. The third is if the link should actually send the opener to the linked resource, or if the opener should just get the focus regardless of what its current page is. The third parameter provides a safe way to close the popup after closing, but still having a link to an existing page if the window isn't actually a popup (such as if the user found the page through a search engine).
When the user clicks on the link, targetopener checks if the browser has the focus command (a few older browsers don't) and if the current window was opened by another window. If these conditions are true, then the opener window gets the focus, the opener is directed to the referenced URL, and the script returns false. Because the function returns false, the link does not go on to the URL (the script has already done that). Note that the link which targets the opener is a little different than the link that opened the popup window to begin with. In this link, onClick says "return goOpener(this)"... the links on the previous pages did not use return.
By default, the popup window stays open but is in the background. If you want the popup to close after going back to the opener, add a second parameter of true to the targetopener function call:
<A
HREF="rbex.html"
onClick="return targetopener(this,true)">main page</A>
We'll begin the tutorial by creating a basic popup window. The technique described here addresses all the major issues in popups. The popup always comes to the front. Different links can target the same popup. The code is simple and easily modified. Everything for the rest of the turorial is a variation on the theme described here. The code in this page creates a popup that is opened from a link. In this section we'll show the code with just the minimal description you need to get it going.
First, copy this script into the <HEAD> section of your page:
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}
//-->
</SCRIPT>
For now we'll skip the details of how the script works, and move to the next step. The script above opens the popup, but something needs to run the script. The most common situation is that the script is run when the user clicks on a link. A link like the following would run the script:
<A
HREF="popupbasic.html"
onClick="return popup(this, 'notes')">my popup</A>
Most of the link is as usual. The URL of the page being linked to is in the HREF attribute. We've added an additional attribute called onClick. Copy the code as it is into your link, with only a small modification. The second argument of the popup() -- 'notes' -- indicates name of the popup window. Every popup window should have its own unique name. Be sure to put the name in single quotes (''). So if you want to name the popup 'stevie' then this would be the code:
<A HREF="popupbasic.html" onClick="return popup(this, 'stevie')">my popup</A>
Read This Next Part Or You'll Go Insane Trying to Figure Out Why Your Popup Doesn't Work
A small but crucial point is often overlooked. The command in onClick must begin with return or the script won't work. Be sure to start the command with return like this:
onClick="return popup(this, 'notes')"
And don't put a space in the page name between the single quotes. If you do, the link will act just like a regular link.
Popup Windows: From an Image Map
In our first variation we'll open the popup from an image map instead of from a regular anchor. We'll use the same script as from our first example. With that script, an <AREA ...> tag in an image map can be made to open a popup in exactly the same way as an <A ...> tag:
<MAP NAME="index">
<AREA
HREF="mypopup.html" ALT="My Popup"
COORDS="10,10,120,120" SHAPE=RECT
onClick="return popup(this, 'gloss')">
<AREA
SHAPE=RECT ALT="Your Popup"
COORDS="140,10,180,50" HREF="yourpopup.html"
onClick="return popup(this, 'gloss')">
<AREA SHAPE=DEFAULT NOHREF>
</MAP>
<IMG SRC="mymap.gif" HEIGHT=130 WIDTH=190 ALT="Image Map Example"
BORDER="0" USEMAP="#index">
Popup Windows: Opening Automatically
In the first two examples (Popup Windows: The Basics and Popup Windows: From an Image Map) the popup is opened when the user clicks on something. In this example the popup opens automatically.
We'll use the same script as in the first example. Copy this script in the <HEAD> section of your page:
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}
//-->
</SCRIPT>
This time, instead of running the script from a link we'll run it from the onLoad attribute of the <BODY ...> tag.
<BODY onLoad="popup('autopopup.html', 'ad')">
The command in onLoad is run when the document is finished loading. Like in our previous example, the command runs popup(), but this time the first argument for popup() is a little different. In the previous example we put this, meaning the link itself, and the script got the URL from the link. In this case there is no link so we pass the actual URL to open. So in our example we put 'autopopup.html'.
Popup Windows: From a Form
For our next variation on the popup theme we're going to open the popup from a form. In this example we're going to change the script around. The following script is custom designed for opening a popup from a form. It works with forms that use both POST and GET. Copy the following script exactly as-is into the <HEAD> section of your document:
<SCRIPT TYPE="text/javascript">
<!--
function popupform(myform, windowname)
{
if (! window.focus)return true;
window.open('', windowname, 'height=200,width=400,scrollbars=yes');
myform.target=windowname;
return true;
}
//-->
</SCRIPT>
Now we'll add some code so that the popup opens when the user submits the form. Add an onSubmit attribute to <FORM ...> tag:
<FORM METHOD=POST
ACTION="../cgi-bin/mypopupcgi.pl"
onSubmit="popupform(this, 'join')">
The first argument for popupform() is always this, meaning the form itself. The second argument, 'join' in this case, is a unique name for the popup.
Popup Windows: Targeting the Opener
Once a popup window has been created, linking from the popup back to the main window (i.e. the window which opened the popup) is a little trickier than might be expected. The problem is that the main window doesn't have a "name" the way the popup window does. Fortunately, JavaScript provides an answer in the form of opener.
To create links in the popup window that target back to the main window, first put this JavaScript in the <HEAD> of the popup page:
<SCRIPT TYPE="text/javascript">
<!--
function targetopener(mylink, closeme, closeonly)
{
if (! (window.focus && window.opener))return true;
window.opener.focus();
if (! closeonly)window.opener.location.href=mylink.href;
if (closeme)window.close();
return false;
}
//-->
</SCRIPT>
A link that uses this script looks like this:
<A
HREF="rbex.html"
onClick="return targetopener(this)">my page</A>
Popup Windows: Closing When They Go to the Opener
In the previous example the link in the popup targets the main page, but the popup stays open in the background after the user clicks on the link. In this page we'll set the link so that it closes the popup after the click.
targetopener takes three parameters. The first is always this, meaning the link itself. The second and third parameters are optional and default to false. (Notice we don't use them in the example above, we'll get to them shortly.) The second parameter indicates if the popup should close. The third is if the link should actually send the opener to the linked resource, or if the opener should just get the focus regardless of what its current page is. The third parameter provides a safe way to close the popup after closing, but still having a link to an existing page if the window isn't actually a popup (such as if the user found the page through a search engine).
When the user clicks on the link, targetopener checks if the browser has the focus command (a few older browsers don't) and if the current window was opened by another window. If these conditions are true, then the opener window gets the focus, the opener is directed to the referenced URL, and the script returns false. Because the function returns false, the link does not go on to the URL (the script has already done that). Note that the link which targets the opener is a little different than the link that opened the popup window to begin with. In this link, onClick says "return goOpener(this)"... the links on the previous pages did not use return.
By default, the popup window stays open but is in the background. If you want the popup to close after going back to the opener, add a second parameter of true to the targetopener function call:
<A
HREF="rbex.html"
onClick="return targetopener(this,true)">main page</A>