However, since all major web browsers now offer popup advertising filters, you must be very careful to do this only in response to a specific user action. Otherwise, the web browser will probably ignore your code or, at best, display a small "popup requested" icon that the user will probably not pay attention to.
<!-- In the head section of the page -->
function wopen(url, name, w, h)
// Fudge factors for window decoration space.
// In my tests these work well on all platforms & browsers.
w += 32;
h += 96;
var win = window.open(url,
'width=' + w + ', height=' + h + ', ' +
'location=no, menubar=no, ' +
'status=no, toolbar=no, scrollbars=no, resizable=no');
When it comes time to actually open such a window, just write a link like the following:
<a href="page.html" target="popup"
onClick="wopen('page.html', 'popup', 640, 480); return false;"> Click here to open the page in a new window. </a>
In this example, the window opens with sufficient interior space to
accommodate a layout 640 pixels wide and 480 pixels tall.
You can easily change the size by changing the width and height
parameters to wopen, above.
The page that opens is
page.html which, of course, you can
also change, but be careful to change both the
attribute and the first argument to
But what does popup mean? This is the name of the new window. If we use the same name for all target attributes and in all calls to wopen, and we have already popped up a new window once, then that same window will be reused on future clicks instead of opening up a new one. This way, you can group together links that should affect the same popup window by using the same window name for them. If you prefer to open a new window every time, use _blank.
When a window is reused by a new click with the same window name, the win.focus() call takes care of making sure the window still comes to the foreground, something that is frequently forgotten in similar scripts.
If you are using XHTML 1.1 Strict, be aware that the
target attribute is no longer accepted. If you
must be XHTML 1.1 Strict compliant, you should remove the
target attribute. A new window will still be opened
followed normally in the same window, and your design should
cope gracefully with this possibility.
For a complete list of the options that can be passed to
window.open to control the behavior of the new
excellent, although I do not recommend following their suggestions
regarding timed popup advertising, as most browsers will ignore it
and the remaining users will be irritated more than the
advertising is worth. Control over new windows is best used to
achieve design goals that improve the user's experience of your site.
Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!