JavaScript: location.href to open in new window/tab?

Javascriptwindow.openwindow.location

Javascript Problem Overview


I have a JavaScript file from a third party developer. It has a has link which replaces the current page with the target. I want to have this page opened in a new tab.

This is what I have so far:

if (command == 'lightbox') {
 location.href="https://support.wwf.org.uk/earth_hour/index.php?type=individual";
}

Can anyone help me out?

Javascript Solutions


Solution 1 - Javascript

window.open(
  'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
  '_blank' // <- This is what makes it open in a new window.
);

Solution 2 - Javascript

If you want to use location.href to avoid popup problems, you can use an empty <a> ref and then use javascript to click it.

something like in HTML

<a id="anchorID" href="mynewurl" target="_blank"></a>

Then javascript click it as follows

document.getElementById("anchorID").click();

Solution 3 - Javascript

Pure js alternative to window.open

let a= document.createElement('a');
a.target= '_blank';
a.href= 'https://support.wwf.org.uk/';
a.click();

here is working example (stackoverflow snippets not allow to opening)

Solution 4 - Javascript

You can open it in a new window with window.open('https://support.wwf.org.uk/earth_hour/index.php?type=individual');. If you want to open it in new tab open the current page in two tabs and then alllow the script to run so that both current page and the new page will be obtained.

Solution 5 - Javascript

For example:

    $(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 6 - Javascript

You can also open a new tab calling to an action method with parameter like this:

   var reportDate = $("#inputDateId").val();
   var url = '@Url.Action("PrintIndex", "Callers", new {dateRequested = "findme"})';
   window.open(window.location.href = url.replace('findme', reportDate), '_blank');

Solution 7 - Javascript

usage of location.href will replace current url with new url i.e https://support.wwf.org.uk/earth_hour/index.php?type=individual in the same webpage.

To open a new tab you can use as below: if (command == 'lightbox') { window.open("https://support.wwf.org.uk/earth_hour/index.php?type=individual";, '_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
QuestioniamjonesyView Question on Stackoverflow
Solution 1 - JavascriptalexView Answer on Stackoverflow
Solution 2 - Javascriptandrew fieldView Answer on Stackoverflow
Solution 3 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 4 - Javascriptuser616639View Answer on Stackoverflow
Solution 5 - Javascriptuser9631725View Answer on Stackoverflow
Solution 6 - JavascriptPrimoshenkoView Answer on Stackoverflow
Solution 7 - Javascriptuser3644678View Answer on Stackoverflow