jQuery: go to URL with target="_blank"

JqueryUrlHrefTarget

Jquery Problem Overview


I am using this bit of jQuery code to get href of the link:

var url = $(this).attr('href');

-- and this bit of code to go to that href:

window.location = url;

Everything is just the way I want it, except the new page opens in the same window as the previous one, and I want it to open in a new window or tab (something that in plain html would have been achieved by using target="_blank" formula).

Question: How can I open the href in the new window or tab with jQuery?

Thank you for your help!

Jquery Solutions


Solution 1 - Jquery

You need to open a new window:

window.open(url);

https://developer.mozilla.org/en-US/docs/DOM/window.open

Solution 2 - Jquery

Use,

var url = $(this).attr('href');
window.open(url, '_blank');

Update:the href is better off being retrieved with prop since it will return the full url and it's slightly faster.

var url = $(this).prop('href');

Solution 3 - Jquery

> Question: How can I open the href in the new window or tab with > jQuery?

var url = $(this).attr('href').attr('target','_blank');

Solution 4 - Jquery

The .ready function is used to insert the attribute once the page has finished loading.

$(document).ready(function() {
     $("class name or id a.your class name").attr({"target" : "_blank"})
})

Solution 5 - Jquery

Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.

	$('.someSelector').bind('touchend click', function() {

		var url = $('a', this).prop('href');
		var target = $('a', this).prop('target');

		if(url) {
			// # open in new window if "_blank" used
			if(target == '_blank') { 
				window.open(url, target);
			} else {
				window.location = url;
			}
		}			
	});

Solution 6 - Jquery

If you want to create the popup window through jQuery then you'll need to use a plugin. This one seems like it will do what you want:

http://rip747.github.com/popupwindow/

Alternately, you can always use JavaScript's window.open function.

Note that with either approach, the new window must be opened in response to user input/action (so for instance, a click on a link or button). Otherwise the browser's popup blocker will just block the popup.

Solution 7 - Jquery

Try using the following code.

$(document).ready(function(){
    $("a[@href^='http']").attr('target','_blank');
});

Solution 8 - Jquery

 url='/controller/action';  
 window.open(location.origin+url,'_blank');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDimitri VorontzovView Question on Stackoverflow
Solution 1 - JqueryChristopher ArmstrongView Answer on Stackoverflow
Solution 2 - JquerybrenjtView Answer on Stackoverflow
Solution 3 - Jquerydefau1tView Answer on Stackoverflow
Solution 4 - JqueryVidit AnjariaView Answer on Stackoverflow
Solution 5 - JqueryrecurseView Answer on Stackoverflow
Solution 6 - JqueryarothView Answer on Stackoverflow
Solution 7 - JqueryLähîrüView Answer on Stackoverflow
Solution 8 - JqueryMahdi PorkarView Answer on Stackoverflow