How to open windows with JavaScript the Right Way
All too often I see web sites open new browser windows using HTML and JavaScript code such as:
<a href="#" onClick="window.open('foo.html');"> <!-- BAD! -->
or:
<a href="javascript:window.open('foo.html');"> <!-- BAD! -->
This is one of my biggest peeves. Here’s why:
Better:
<a href="foo.html" onClick="window.open('foo.html');return false;">
This solves all of the problems above. The “return false
” is important! When the onClick
event handler returns false, a browser does not follow the href
link.
Better yet:
Re-evaluate why you want to open links in new windows at all. Most browsers have an “Open in New Window” command; if users want new windows, let them ask for it.
No Comments Yet »
RSS feed for comments on this post.