Opening new windows with JavaScript, version 1.1
Update: The technique described here is not ideal. Read why in New windows with JavaScript and the target attribute.
After considering the suggestions made in the comments on my article Using JavaScript instead of target to open new windows and thinking some more about it, I’ve made some changes to the script. For the full story, please read the original article if you haven’t already.
The revised script differs from the original in two ways:
- It no longer opens a new window if a modifier key is pressed while the link is clicked. Good news for people who like to ctrl/shift/cmd/whatever-click to open links in new tabs.
- It now checks if the new window was successfully opened. If it was not, it returns true to allow the browser to open the link in the original window.
To enable link styling depending on the type of file being linked to, I have revised the markup to use an additional class name that matches the file type. This has the additional benefit of enabling link styling in browsers with no JavaScript support. Note that I haven’t styled the link in the demo document.
Example markup:
<a href="javascript-target.pdf" class="non-html pdf">A sample PDF file</a>
Try it out in the JavaScript target demo document.
Here is the revised script, which should be included in your common.js or wherever you store your global JavaScript functions:
// Create the new windowfunction openInNewWindow(e) {var event;if (!e) event = window.event;else event = e;// Abort if a modifier key is pressedif (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {return true;}else {// Change "_blank" to something like "newWindow" to load all links in the same new windowvar newWindow = window.open(this.getAttribute('href'), '_blank');if (newWindow) {if (newWindow.focus) {newWindow.focus();}return false;}return true;}}// Add the openInNewWindow function to the onclick event of links with a class name of "non-html"*/function getNewWindowLinks() {// Check that the browser is DOM compliantif (document.getElementById && document.createElement && document.appendChild) {// Change this to the text you want to use to alert the user that a new window will be openedvar strNewWindowAlert = " (opens in a new window)";// Find all linksvar objWarningText;var link;var links = document.getElementsByTagName('a');for (var i = 0; i < links.length; i++) {link = links[i];// Find all links with a class name of "non-html"if (/\bnon\-html\b/.test(link.className)) {// Create an em element containing the new window warning text and insert it after the link textobjWarningText = document.createElement("em");objWarningText.appendChild(document.createTextNode(strNewWindowAlert));link.appendChild(objWarningText);link.onclick = openInNewWindow;}}objWarningText = null;}}
Either copy and paste from here or download my sample javascript-target.js file, which contains all the JavaScript you need to use this.
You also need to make sure the getNewWindowLinks() function is executed when the document has loaded. Use whichever addEvent() function you prefer using:
addEvent(window, 'load', getNewWindowLinks);
That line is included in the javascript-target.js file that accompanies this article.
If you have any suggestions for further improvement, please post a comment.
Update: I have updated the script again. More information is available in Opening new windows with JavaScript, version 1.2.
- Previous post: Testing in both IE 6 and IE 7
- Next post: Accessibility statements and site viewing options
Subscribe / follow
Sponsors
Authentic Jobs
- Dev Ops Engineer @ General Things at The Sourcery (San Francisco, CA, Ca, US)
- Web Designer/Web Designer Senior at University of Michigan (Ann Arbor, Mi, Mi, US)
- Data Visualization Graphic Designer at Ultimate Fighting Championship (UFC) (Las Vegas, Ne, US)
- Front End Developer at Domani Studios (Brooklyn, NY, Ne, US)
DreamHost web hosting
Use the promo code 456BEREASTREET3 to save USD 20 when you sign up for DreamHost


Comments
Some excellent additions imo!
Checking whether the modifier key is pressed is a great idea.
I still prefer the rel attribute for this.
When I hold Ctrl while clicking on the link in Firefox, it opens the page twice.
I would think that you should wrap your functions up using object literal notation for safekeeping.
Good changes with this version, very useful.
trovster: I considered the rel attribute, but after reading up on it I’m not convinced that using it for this purpose is correct.
Jero: Drats, you’re right. It only happens in Firefox for Windows though, so I’m not sure what’s up with that.
Jeff: I’ll look into that if my brain can understand the concept. I am not OOP compatible.
ctrlkey should be ctrlKey (uppercase K)
Maybe it’s smart to null newWindow afterward. I’m not sure though if there is any a memory leak.
Ok, it works in Firefox and other browsers that use the Ctrl key to open links in a new tab now. I’d written “ctrlkey” instead of “ctrlKey”. Duh.
I see just beat me to it :-)
Roger have you ever seen this - http://test.newplasticarts.co.uk/flag-offsite-links/ ? I know its not exactly what you were aiming at but has some interesting ideas. :)
Sorry if this was posted in the last entry.
Ryan: Nope, I missed that one, and it does have some interesting ideas. Thanks!
The last time I had to discuss opening links in a new window with a client, the client tried to click the back button on the new window. I was so tempted to open a bag of “I told you so!”
What happens if a person has the configuration to launch in new window vs. new tab? Does it still open a new window (i.e. sized window for Flash)?
I was relieved too note that you managed to prevent the double window from ctrl-clicking but your script is still broken.
When doing a regular click it doesn’t even work for me, it just opens the “popup” in the same window as the page i click it from. Using Firefox 1.5. Might be one of my extensions, but it hasn’t stopped other popups from working.
Ryan: It opened in a new tab for me, I have FF set to open all new links and popups in tabs :)
Spot on Roger!
Paul Solecki: In that case whats the problem? A new tab is better than a new window, if a site opens pages in a new window instead of a new tab with me I go nuts! heh
I like the toggle option though, I have to admit I haven’t had a chance to fully look over Roger’s solution so I have no idea if he has a toggle option also.
Ryan: It depends on the browser and how it is configured, if there are any extensions installed etc.
Raevel: It must be an extension. It works in Firefox here, both Mac OS X and Windows.
Ryan: This script does not contain a toggle option, but it sounds like a good idea. Maybe in the next version ;-).
Sorry, but why don’t you just use XHTML 1.0 Transitional? at least in the pages where you have to use the target attribute?
I myself, use Strict mode too, but if I have to use target attribute, I think I’ll use Transitional mode.
That’s what I’ve done with an old script that uses a form with the “name” attribute. Sure It’s a bit lazy :)
But, that’s easier and IMHO harmless.
Thanks.
Ok, i watched the 2 topics about javascript targets for a couple of days. But i cannot see the reason why you should javascript to init a target method.
On the otherhand why not do this target magic in CSS:
a[target=”_blank”] {color: #000000}
Or am i missing the point here?
function blank_magic() {
var show = document.getElementsByTagName(“a”);
for ( var j=0; j < show.length; j++ ) {
if (anchor.getAttribute(“id”) == “external”)
}
window.onload = blank_magic;
LINK:
id=”external”
karim: Because I don’t know which pages will need to open new windows. Clients handle the content through a CMS. So that is not an option.
Jungsonn: Because it is behaviour.
I’m not sure what the purpose would be of making all links with target=”blank” black. Care to explain that?
The script you entered will only work for a single link on a page. Id has to be unique.
Yeah i needed more coffee, those nightly posts :) I got the wrong idea of what you supposed to do.
The id attribute, if replaced with “rel” would that work for all links? think it does.
Just a sec… “id” does work on all links.
This time i tested my code:
a href=”http://www.foo.com” id=”external”
a href=”http://www.foobar.com” id=”external”
a href=”http://www.barfoo.com” id=”external”
If it works it is a coincidence. I wouldn’t be surprised if some browsers will only find the first or last link with the same id. Either way it is invalid, which rules it out.
I just made up “id” as atribute for example, do you think that rel=”external” would be valid?
W3c has the following information about rel & id on their website.
id: “When the name or id attributes of the A element are set, the element defines an anchor that may be the destination of other links.”
rel: “This attribute describes the relationship from the current document to the anchor specified by the href attribute. The value of this attribute is a space-separated list of link types.”
So IMHO i think both can be used. I agree on your argument that ‘id’ is very clever chosen, and could produce flaws. So the attribute ‘rel’ should be an alternative.
Roger, it would seem you’ve made a slight mistake …
should read:
It should return false even if focus method is unsupported.
Jungsonn: I am still undecided on the use of the rel attribute. The id can be used, but it is very limited since any id is only allowed once on a page. You cannot have multiple links that share the same id.
Michaël: Yes it should. Thanks for catching that mistake.
IMHO
is not neccessary. Instead of:
you can write:
I found a small problem in that firefox (Win 1.5) fired a javascript warning
when it encountered the line:
To avoid the warning I made a small modification, replacing lines 3 to 5 with:
Roger, Did you see this tutorial at Accessify.com? http://accessify.com/features/tutorials/the-perfect-popup/
endryou, james: Thanks, I’ll update the script.
Marc: Nope, missed that one. I do prefer my approach though ;-).
@Marc
That script looks much like the one i wrote, although slightly different. Sure you can use this one, just like mine. Although IMO you should see it as “a quick fix”. Roger’s one is more covering.
Nice work, Roger, really enlightening approach to light- weight/ unobtrusive AND fallback- capable JS. Thus I’m happy to report that both versions’ demos open fine (= render to same window) in Blazer 4.3 Mozilla 4.0- compatible browser (accessed through google.com/gwt/n mobile proxy) on a PalmOS Lifedrive PDA with 320x448px viewing area; this browser/ proxy combo otherwise being pretty unforgiving where abuse —or simply excess— of JS is concerned (I was able to preview, but unable to post this comment via the proxy-mt.cgi, though that may be a limitation on MT-comments’ backend; ergo posting it directly.)
Nice script, thanks for sharing!
I’m trying to extend the script to include custom window properties. I’m a js newbie, so here goes.
Basically I have a custom function called “openPromo”.
function openPromo() { var p = document.getElementById('promo'); var config = {status:0,...,target:'_blank'}; p.onlclick = openInNewWindow }My question is, how do you pass the “config” variable into the “openInNewWindow” function. I guess the “e” parameter is confusing me as I don’t know where it comes from.
Thanks!
Answered my question above. For anyone who may be curious.
function setOpenInNewWindow(config) { return function openInNewWindow(e) { /* Rogers function with Michaël Guitton suggestions */ } } var config = [window settings] element.onclick = setOpenInNewWindow(config);See Michaël Guittons comment
sorry for cross-post, but I believe this to be relevant for this entry too: previous comment
Thanks for code.
Trying to get the pdf to open in a new window with “target=blank” worked on my own machine but not when I uploaded to my site. And I only wanted it for one of a number of links on the page.
Keep up the good work.
I use this which is the bare minimum, consider accessibility and warn users on the new window:
<a href=”cat.jpg” onclick=”this.target=’_blank’” onkeypress=”this.onclick” title=”Cat photo (New Window)”>View the cat</a>
If Javascript is disabled the image opens in the current window, I believe there are no alternatives if you want to stay with Strict doctype.
This is an extreme shortcut if you don’t want to load other pieces of javascript which will introduce more accessibility warnings…
Hello,
I am opening the Excel, Word and PDF documents from my ASP .NET script.
Whenever I open a document, it’s displaying the modal Open Save Cancel.
I would like to open the documents by suppresing the dialog.
Here is the code.
function popdocument(itemCode,dir) { document.execCommand(‘SaveAs’,false,itemCode) }
Any help in suppressing the dialog will be greatly appreciated.
Thanx,
J
This is why I love jQuery for things like this…
$(“a.newWindow”).attr({target: “_blank”});
Only 1 short line of code.
Eric: Yeah, one line of code plus an entire script library…
Roger: Yes if you are only using jQuery to open a new window otherwise extremely good if like me, you use it for other reasons.
How can I change the script to open the window in a new TAB (IE 7.0 and Latest Firefox)?
Is there a special attribut I have to set?
Not possible as far as I know.
That would be a user-side preference for what behaviour the visitor chooses of the web client.
Great script!
A small remark: the comments in the downloadable sample script say that it uses the class name new-window, but it tests for non-html.
Comments are disabled for this post (read why), but if you have spotted an error or have additional info that you think should be in this post, feel free to contact me.