Login  Register 
August 01, 2010
:: Resources» Fingerfuel Blogs» Andy Hock's Blog   Search
Search
  
Andy Hock's Technology Blog
Feb 17

Written by: Andy Hock
2/17/2007 7:30 PM

I have a ton of code snippets I store in my Office One Note. Unfortunately, I couldn't find my handy javascript window.open script that one can place directly inside an href tag. So I decided to google and find a script. An hour later, after having found at least twenty links that claimed to show how to create a new window that either do not work at all, or don't work correctly in IE and/or Firefox or Safari, I figured out again how to create a cross-browser window open.


The many incorrect tutorials claim things like putting the <A onclick="Javascript:whatever()" href="#null">...</A> and so on, just do not work.  I don't know where this myth started, but there are at least a dozen tutorials that assert this method to work. You'll just end up with a javascript error in any IE browser.

The correct way, assuming you want to open the window in a new window, that is cross-browser compatible for IE, Firefox, Opera, Safari and others is the following:


<A onclick="Javascript:window.open('/yourfolder/yourpage.aspx' , '_blank', 'height=600, width=800, resizable=yes');return false;" href="http://yourdomain.com/yourfolder/yourpage.html">here</A>

That's it.  Don't believe any tutorial that tries to tell you otherwise.  The key is to use the name "_blank" in the name parameters.  Too many people think that the 'name' is for the page title name or something.  It's not.  It's the equivalent of the 'target="_blank"' parameter of the href tag. Also, you can add other settings other than just resizable, like scrollbars, menubar and others. You can find the correct parameters for an href tag here, but as with many of the other tutorials, the tutorial is incorrect in the way it uses the 'name' parameter, using it as a page title name, when in fact it's meant to describe the target (in this case, the incorrect tutorial uses 'popupwindow', when it should use '_new' or '_self' or '_blank'.


It's also important to include the 'return false;' javascript snippet, or you will get unintended results in Firefox, like opening up two windows instead of one.

Tags:

4 comments so far...

Re: The many incorrect tutorials on how to open a javascript window.

Your are my best mate now after reading this, how many times I have tried and failed to understand the other tutorials. Many thanks for saving me from madness :-)

By Steve Nixey on   7/4/2007 12:10 PM

Re: The many incorrect tutorials on how to open a javascript window.

Wow, this works fine in a DNN module, without the return=false though.

By Willy David Jr on   7/9/2007 7:34 AM

Re: The many incorrect tutorials on how to open a javascript window.

Hey Steve. Thanks for the comment. I know how frustrating it is reading all the stuff out there that isn't quite right.

By AndyHock on   7/9/2007 7:36 AM

Re: The many incorrect tutorials on how to open a javascript window.

Hey Willy:

Well, if you actually check it w/Firefox, you'll find that it dependes on what you mean by 'works fine', because if you leave out the 'returns false', in Firefox, it wil open up a blank window every time you click on the javascript window.

This is why it's critical that we should test for at least Mozilla and IE (I test for Opera too) browsers every time--or comments show up on a blog that things work fine and people will start believing them. :)

By AndyHock on   7/9/2007 7:41 AM

Your name:
Title:
Comment:
Add Comment    Cancel  
  
Blog Archives
  
:: Resources» Fingerfuel Blogs» Andy Hock's Blog
Copyright © 2004-2007 by Fingerfuel.com.