Advertisement
Advertisement



Question

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?

2013/01/08
1
94
1/8/2013 5:08:46 PM

Accepted Answer

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.

2010/05/13
204
5/13/2010 2:46:40 PM

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

$('a#link_id').click(function() {
    $(this).attr('target', '_blank');
});
2011/11/17


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

$(function(){
  $('yourselector').prop('target', '_blank');
}); 
2012/12/03

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.

2013/12/10

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

2013/12/23

Source: https://stackoverflow.com/questions/2827637
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]