javascript window.location in new tab

JavascriptHtmlRedirect

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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 :)

Solution 2 - Javascript

You can even use

window.open('https://support.wwf.org.uk', "_blank") || window.location.replace('https://support.wwf.org.uk');

This will open it on the same tab if the pop-up is blocked.

Solution 3 - Javascript

I don't think there's a way to do this, unless you're writing a browser extension. You could try using window.open and hoping that the user has their browser set to open new windows in new tabs.

Solution 4 - Javascript

This works for me on Chrome 53. Haven't tested anywhere else:

function navigate(href, newTab) {
   var a = document.createElement('a');
   a.href = href;
   if (newTab) {
      a.setAttribute('target', '_blank');
   }
   a.click();
}

Solution 5 - Javascript

with jQuery its even easier and works on Chrome as well

$('#your-button').on('click', function(){
       $('<a href="https://www.some-page.com" target="blank"></a>')[0].click();    
})

Solution 6 - Javascript

Rather going for pop up,I personally liked this solution, mentioned on this Question thread https://stackoverflow.com/questions/5141910/javascript-location-href-to-open-in-new-window-tab/5141926

$(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.

Solution 7 - Javascript

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.

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
QuestionMuhammad Imran TariqView Question on Stackoverflow
Solution 1 - Javascriptuser1450789View Answer on Stackoverflow
Solution 2 - JavascriptFanis MahmalatView Answer on Stackoverflow
Solution 3 - JavascriptIan OxleyView Answer on Stackoverflow
Solution 4 - Javascriptrodrigo-silveiraView Answer on Stackoverflow
Solution 5 - Javascriptrelief.meloneView Answer on Stackoverflow
Solution 6 - JavascriptOyeHarishView Answer on Stackoverflow
Solution 7 - JavascriptSubbuView Answer on Stackoverflow