I am diverting user to some url through window.location
but this url opens in the same tab in browser. I want it to be open in new tab. Can I do so with window.location? Is there another way to do this action?
165
votes
7 Answers
47
votes
508
votes
window.open('https://support.wwf.org.uk', '_blank');
The second parameter is what makes it open in a new window. Don't forget to read Jakob Nielsen's informative article :)
35
votes
18
votes
10
votes
5
votes
Rather going for pop up,I personally liked this solution, mentioned on this Question thread JavaScript: location.href to open in new window/tab?
$(document).on('click','span.external-link',function(){
var t = $(this),
URL = t.attr('data-href');
$('<a href="'+ URL +'" target="_blank">External Link</a>')[0].click();
});
Working example.
0
votes
We have to dynamically set the attribute target="_blank" and it will open it in new tab.
document.getElementsByTagName("a")[0].setAttribute('target', '_blank')
document.getElementsByTagName("a")[0].click()
If you want to open in new window, get the href link and use window.open
var link = document.getElementsByTagName("a")[0].getAttribute("href");
window.open(url, "","height=500,width=500");
Don't provide the second parameter as _blank in the above.
window.location
a requirement? Or can other JS solutions be offered ? – Khez