94

I have a click handler for a specific link, inside that I want to do something similar to the following:

window.location = url

I need this to actually open the url in a new window though, how do I do this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Chris
  • 26,744
  • 48
  • 193
  • 345

10 Answers10

208

You can like:

window.open('url', 'window name', 'window settings')

jQuery:

$('a#link_id').click(function(){
  window.open('url', 'window name', 'window settings');
  return false;
});

You could also set the target to _blank actually.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • but this jquery code will not navigate to the target automatically – Amr Elgarhy May 13 '10 at 14:42
  • 26
    _blank is the default target, so using window.open(url) should suffice – themerlinproject Dec 06 '11 at 03:10
  • Not sure will help and not exactly same issue but I was searching for same solution to **download** a file (not from a link but a button) and on Chrome the window didn't open and no download until I simply change to window.location = 'url' which doesn't change location but download the file... – gdoumenc May 14 '14 at 13:09
  • window.open(url) is fine :) – fudu Nov 05 '18 at 08:07
34

Here's how to force the target inside a click handler:

$('a#link_id').click(function() {
    $(this).attr('target', '_blank');
});
chadwackerman
  • 804
  • 12
  • 9
  • 5
    No need of using jQuery selector in the click handler so line `$(this).attr('target', '_blank');` could be changed to `this.target = "_blank";` Also, if the anchor links on the page can be modified to have `rel="external"` attributes then you could create a global click handler for the page with the jQuery selector `a[rel="external"]` rather than having a click handler per link selected with `a#link_id` – himanshu Sep 12 '12 at 20:35
17

you will need to use window.open(url);

references:
http://www.htmlcodetutorial.com/linking/linking_famsupp_120.html
http://www.w3schools.com/jsref/met_win_open.asp

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
  • 5
    Yikes! Don't use or link to w3schools, it's NOT associated with the W3C. Use MDN instead: https://developer.mozilla.org/en-US/docs/Web/API/Window.open – A.B. Carroll Jul 02 '14 at 15:53
  • 5
    w3schools is good (ignore the 'few' w3c trolls)... will continue to be AN authoritative source...even w3c now are backing it again ;-) – Dawesi Aug 26 '15 at 07:51
5

You can also use the jquery prop() method for this.

$(function(){
  $('yourselector').prop('target', '_blank');
}); 
Usha
  • 156
  • 2
  • 5
2

I just found an interesting solution to this issue. I was creating spans which contain information based on the return from a web service. I thought about trying to put a link around the span so that if I clicked on it, the "a" would capture the click.

But I was trying to capture the click with the span... so I thought why not do this when I created the span.

var span = $('<span id="something" data-href="'+url+'" />');

I then bound a click handler to the span which created a link based on the 'data-href' attribute:

span.click(function(e) {
    e.stopPropagation();
    var href = $(this).attr('data-href');
    var link = $('<a href="http://' + href + '" />');
    link.attr('target', '_blank');
    window.open(link.attr('href'));
});

This successfully allowed me to click on a span and open a new window with a proper url.

Dramoth
  • 61
  • 1
  • 6
1

this solution also considered the case that url is empty and disabled(gray) the empty link.

$(function() {
  changeAnchor();
});

function changeAnchor() {
  $("a[name$='aWebsiteUrl']").each(function() { // you can write your selector here
    $(this).css("background", "none");
    $(this).css("font-weight", "normal");

    var url = $(this).attr('href').trim();
    if (url == " " || url == "") { //disable empty link
      $(this).attr("class", "disabled");
      $(this).attr("href", "javascript:void(0)");
    } else {
      $(this).attr("target", "_blank");// HERE set the non-empty links, open in new window
    }
  });
}
a.disabled {
  text-decoration: none;
  pointer-events: none;
  cursor: default;
  color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<a name="aWebsiteUrl" href="http://www.baidu.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href=" " class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.alibaba.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.qq.com" class='#'>[website]</a>
Scott 混合理论
  • 2,263
  • 8
  • 34
  • 59
0

Microsoft IE does not support a name as second argument.

window.open('url', 'window name', 'window settings');

Problem is window name. This will work:

window.open('url', '', 'window settings')

Microsoft only allows the following arguments, If using that argument at all:

  • _blank
  • _media
  • _parent
  • _search
  • _self
  • _top

Check this Microsoft site

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • 4
    -1: Your licence-violating copy/paste from http://stackoverflow.com/a/1462500/560648 notwithstanding, that's not true. [The argument _is_ supported](http://msdn.microsoft.com/en-us/library/ms534659%28v=vs.85%29.aspx). It just can't contain spaces or dashes or other punctuation. Read other answers and comments on that question. – Lightness Races in Orbit Jan 08 '13 at 16:40
0

Be aware if you want to execute AJAX requests inside the event handler function for the click event. For some reason Chrome (and maybe other browsers) will not open a new tab/window.

0

This is not a very nice fix but it works:

CSS:

.new-tab-opener
{
    display: none;
}

HTML:

<a data-href="http://www.google.com/" href="javascript:">Click here</a>
<form class="new-tab-opener" method="get" target="_blank"></form>

Javascript:

$('a').on('click', function (e) {    
    var f = $('.new-tab-opener');
    f.attr('action', $(this).attr('data-href'));
    f.submit();
});

Live example: http://jsfiddle.net/7eRLb/

0

What's wrong with <a href="myurl.html" target="_blank">My Link</a>? No Javascript needed...