Close a subwindow
Takeaway: With JavaScript code, you can close a subwindow when the opener file closes. Here's how.
By Brent Lee Metcalfe
You can close a subwindow when the opener file closes. The code for doing this may be quirky, but it works!
Once you've launched the new widow, click the Reload or Refresh button to close it. The code goes something like this:
var subWin = null;
function popOne() {
function eradic8() {
<body … onUnload="eradic8()">
I know, I know, there's no such thing as a window open property. Tell it to the JavaScript parsers. If you don't include this property, you run the risk of generating an error in 4.x versions of IE and Communicator--especially if the user manually closes the subwindow.
To automatically close a JavaScript-generated window (or windows) when another window is focused, use onFocus to trigger window shutdown instead of onUnload. This is the trick used to close several of the example windows in this article.
Why bother to automatically close windows? Users should not be forced to do more than they have to. If you compel a site visitor to open a new window, you should automatically close the window without user intervention.
Another method
You can also close a subwindow when the opener window object closes.
The code for doing this is quite different from the code used in Tip 9, and is Communicator-specific; it doesn't work in IE.
Because the Builder.com site is a frames-free environment, the example dependent window won't close until the browser is exited.
Since frames are window objects, too, dependent behaves differently with framesets; dependent subwindows launched from a frame will close when the frameset is exited.
Including dependent as a feature in the window open() method lets users automatically close a subwindow when the window object that launched it closes. For example:
function playItAgain() {
Notice that when a new window is specified as a dependent the title font is less bold, and the title bar Navigator icon and minimize and resize buttons are removed.
Non-dependent subwindow:
Dependent subwindow:
Brent Lee Metcalfe is a Web architecture consultant.
subWin = window.open('yourURL.html','aWin','height=200,width=200');
}
if (subWin != null && subWin.open) subWin.close();
}
popWin = window.open('aurl.html','win','dependent,resizable,top=20,left=20,height=200,width=200');
}
SponsoredWhite Papers, Webcasts, and Downloads
- Getting the Foundation Right - Unified Communications ShoreTel
- SprintSecure Message Protection Fact Sheet Sprint
- Software Development's Classic Mistakes 2008 Construx Software Builders
- Live Webcast: The 2008 Email Security Benchmarking Report Google
- 10 Deadly Sins of Software Estimation Construx Software Builders
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





