Kevin Lynch Archives

contact: contact

February 22, 2003

Opening windows with JavaScript


Clicking on the photos in the left margin of my site will open a new window to display a larger version. This feature is implemented using JavaScript, which varies across browsers and platforms. Jason Kottke pointed out (thanks!) how to make this work better across current browsers such as Chimera — if you like reading about JavaScript, please click more for details.

I'm using Dreamweaver's Open Browser Window behavior, and attaching it to an image by wrapping it with an href using #, which in many browsers means don't go anywhere:

<a href="#"
onclick="MM_openBrWindow('med/DSCN2062.jpg',
'Photo','width=800,height=600')">
<img src="DSCN2062.jpg"
width="120" height="90" border="0" alt="" /></a>

The problem with this is that in some browsers (like Chimera), the # link causes the page to reload and reset to the top of the page in addition to opening the new window. This is of course confusing when you go back to reading the main page. Also, if your browser doesn't support JavaScript, you don't get to see a bigger image since the # link doesn't go anywhere.

A fix for this is to change the # to instead link to the image URL:

<a href="med/DSCN2062.jpg"
onclick="MM_openBrWindow('med/DSCN2062.jpg', 
'Photo','width=800,height=600')">
<img src="DSCN2062.jpg"
width="120" height="90" border="0" alt="" /></a>

This gets closer, but on some browsers (like Internet Explorer on OS X) this now causes the image to load on the main page in addition to loading a copy in a new window. To prevent that from happening, you can add a 'return false' instruction at the end of the onClick handler:

<a href="med/DSCN2062.jpg"
onclick="MM_openBrWindow('med/DSCN2062.jpg', 
'Photo','width=800,height=600'); return false;">
<img src="DSCN2062.jpg"
width="120" height="90" border="0" alt="" /></a>

This makes the link work much better across a wider number of browsers. I've let the Dreamweaver team know about this issue so we can improve this by default.

Update: ranjan has pointed out a Dreamweaver extension to do this, which creates a little bit better code yet -- instead of needing to repeat the same URL twice to open, you can just put it in the href once and use JavaScript to get that value for the open window call:

<a href="med/DSCN2062.jpg"
onclick="MM_openBrWindow(this.href, 
'Photo','width=800,height=600'); return false;">
<img src="DSCN2062.jpg"
width="120" height="90" border="0" alt="" /></a>
22 Feb 03 11:51 AM

Comments

Danger says:

Thanks,I think can make a new open browser window behavior extension.

Posted by: Danger on 22 Feb 03 05:18 PM

John Kranz says:

Thanks, Kevin, for passing on the tip!

Posted by: John Kranz on 23 Feb 03 01:16 PM

Joseph Lowery says:

Another, more general option, is to use javascript:; instead of the hash mark as the "link" when you want to trigger a behavior.

Posted by: Joseph Lowery on 23 Feb 03 01:23 PM

Kevin Lynch says:

Cool, be great to have an extension do this.

Joseph, good to hear from you! Yes, javascript: as the link is good too, but doesn't do anything on browsers that don't support javascript or if the user has turned off javascript.. using onclick and a normal link in the href makes it work in both situations

Posted by: Kevin Lynch on 23 Feb 03 02:35 PM

jkottke says:

Plus, javascript: is deprecated.

Posted by: jkottke on 24 Feb 03 06:11 AM

Phillip Harrington says:

I'm opposed to the # href. It's useless for non javascript browsers. Why not offer them the link to the page without the popup? Why should they suffer?

Posted by: Phillip Harrington on 24 Feb 03 10:32 AM

Phillip Harrington says:

I meant that comment to agree with you. Of course I railed on this issue a long time ago (http://philsown.com/articles/javascript-links ).

Posted by: Phillip Harrington on 24 Feb 03 10:34 AM

Michael says:

I've always found this one useful:

http://www.flash-db.com/PopUp/

Posted by: Michael on 25 Feb 03 12:24 AM

ranjan says:

There is an extension for this at http://www.flevooware.nl/dreamweaver/

Posted by: ranjan on 26 Feb 03 07:50 PM

manuel razzari says:

Great to see someone at macromedia has finally realised this and the default behavior will hopefully be fixed in the next release of DW...

Posted by: manuel razzari on 09 Mar 03 11:20 AM

Kevin Lynch says:

Yes, the Dreamweaver team is on it now. If you see other things that the team should be aware of, please do send a note at http://www.macromedia.com/support/email/wishform/?6213=6 -- this gets to people directly on the product team.

Posted by: Kevin Lynch on 09 Mar 03 11:39 AM

Gerald says:

A similar story could be found at http://www.klynch.com/archives/000030.html. I would like to mention that search engine spiders fall into the "non-javascript client" category, thus providing accessible links enables them to gather this images.

Posted by: Gerald on 27 Mar 03 03:08 AM

Gerald says:

Ooops, I made a copy&paste error, the suggested link was http://www.evolt.org/article/comment/17/20938/index.html.
Sorry. (What's about a Preview functionality?)

Posted by: Gerald on 27 Mar 03 03:13 AM

Comments on this entry are now closed

You can of course make comments in your own blog, and Trackback continues to be available to reference your post here.