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,
"return goOpener(this)"... the links on the previous pages did not use
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:
onClick="return targetopener(this,true)">main page</A>
which creates the link in
this popup window.