We Recommend

HTML: The Definitive Guide HTML: The Definitive Guide
They teach you that learning HTML is like learning any other language and that reading a book of rules can only take you so far. Readers begin writing what may be their first Web page just two pages into the book's second chapter. From there on, they provide a wide range of HTML coding to allow readers to learn from good examples. The book includes a handy "cheat sheet" of HTML codes for quick reference.


Posted By

1man on 07/08/06


Tagged

xhtml open new


Versions (?)


Who likes this?

55 people have marked this snippet as a favorite

jonhenshaw
luman
designerd
arturo
n2linux
saveasraw
ndegruchy
sendoa
jkochis
j4k
kgosser
gensmann
kyokutyo
Quinnin
yuconner
dojob
pablodgavilan
marlfoskr
yu_industries
koncept
mountchuck
jacksont123
hyperspace
cynic68
mozillalives
hollowmyth
manub
maid450
rich13
bbhack
robotoverlord
SamuelMiller
marteki
bycoffe
Winkyboy
blueshome
willgarrison
fukami
jbreen
d4rk
jamesbazemorenwacom
gbot
markor
paullorentzen
zeljkoprsa
gAmUssA
Arzakon
unravelme1
MondoMan
alvaroisorna
panatlantica
juliend2
esquareda
jfherring
hans


Standard Compliant Way to Open a New Page (no target tag)


Published in: HTML 


Very simple standard compliant way of opening a page in a new window. Since XHTML 1.0 Strict doesn't allow the target attribute in the tag. This method isn't recommended, use the DOM folks! See "Standard Compliant Way to Open a New Page Function" in my other snippets of code.

  1. <a href="http://address.com/" title="Title Here" onclick="window.open(this.href,'newwin'); return false;">Insert Link Title</a>

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: kaiser79 on January 18, 2008

smaller, fail-safe script: return !window.open(this.href,'newwin');

Posted By: noncon on March 10, 2008

That's all good and everything but you shouldn't be forcing new windows if the user doesn't want it. I especially hate it as well. If I want to open a link in a new window I already know how to do that.

You should only ever use it if you let the user know the link will open in a new window.

Posted By: 1man on March 10, 2008

I Agree :)

Posted By: jatkins on March 20, 2008

I agree with noncon.

It's extremely annoying. Seriously, whenever I site wants to open a new window on me - let's say NASA, they do it a lot - I right click, copy the URI (which usually is a JavaScript call, i.e. javascript:openNASAWindow('externalflash/sts-1/index.html');, open a new tab, paste it into the address field, trim off the openNASAWindow() function, add http://www.nasa.gov/ to the beginning of the URI, and visit the paste. It doesn't take very long, but it's extremely irritating, and very bad usability.

Whilst I agree that if you MUST open a new window, you should use this method, I also believe that you should NEVER open a new window - yes, contradicting my first statement. In 2002, you might have wanted this code, but not now. EVERY major browser supports tabs, including just about every non-major browser. That's IE7 (and 8), Firefox, Opera, Safari. Also, OmniWeb, and Konqueror.

Posted By: neal_grosskopf on May 16, 2008

Could this be trimmed to this? window.open(this.href); return false

Posted By: 1man on May 19, 2008

Hi Neal, Yes it can be trimmed down to that as the window name is optional. See here http://www.w3schools.com/HTMLDOM/metwinopen.asp

As has been mentioned before though, i wouldn't use this method to open a window, i'd use the DOM, make all your javascript unobtrusive. It's not recommended that you open a new link in a new window, if a user wishes to do this they can make that choice. If you are opening in a new window atleast give some input to the user that this will happen.

You need to login to post a comment.