jQuery: go to URL with target="_blank"

Dimitri Vorontzov picture Dimitri Vorontzov · Jul 13, 2011 · Viewed 247.9k times · Source

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!

Answer

Christopher Armstrong picture Christopher Armstrong · Jul 13, 2011

You need to open a new window:

window.open(url);

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