506

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?

7 Answers 7

1245
window.open(
  'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
  '_blank' // <- This is what makes it open in a new window.
);
10
  • 118
    Worth to mention: Whether a new tab or window is created, is decided by the browser (setting).
    – jAndy
    Feb 28, 2011 at 12:25
  • 5
    @alex I edited this answer to make the important bit readable. If you don't approve of my formatting, may I suggest shortening the URL so that the second parameter fits within the unscrolling region?
    – Phrogz
    Sep 20, 2011 at 17:51
  • 7
    @ErickBest That's incorrect. "_blank" guarantees that the window/tab will be new. Anything else (besides the other special names) are giving that window the specific name, and subsequent links to that target will reuse the window. jsFiddle.
    – alex
    Jul 24, 2013 at 9:39
  • 4
    This just triggers a pop up block notification in modern browsers, doesn't simulate a _blank anchor click at all. Oct 1, 2014 at 12:46
  • 34
    popup prevented by browser. Mar 17, 2015 at 13:29
50

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)

5
  • 2
    Triggers Chrome's built-in popup blocker in the latest Chrome here on Jan 4, 2022.
    – Volomike
    Jan 4, 2022 at 14:54
  • @Volomike In my chrome version is 96.0.4664.110 (Official Build) (arm64) (MacOs) - example works. What is you chrome version? Jan 4, 2022 at 14:58
  • I am on Windows 10. Version 96.0.4664.110 (Official Build) (64-bit). When I click your example hyperlink, Chrome intercepts and blocks the popup and I get an alert on my address bar. screencast.com/t/b6wA1Vay
    – Volomike
    Jan 4, 2022 at 15:20
  • Perhaps you enabled popups from jsfiddle.net. That might be why you don't see the popup blocking.
    – Volomike
    Jan 4, 2022 at 15:41
  • 1
    This solution is better than window.open since it can work in safari
    – hatched
    Jul 5, 2022 at 5:55
39

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();
8
  • 16
    I was hopeful for this solution, however it does still seem to trigger the pop-up block (at least in Chrome). I have a feeling that the browser is aware that it's a javascript click and treats it differently. Oct 1, 2014 at 12:44
  • 1
    Thank you Nathan. You're quite correct. You still get a pop up block message. I thought I'd cured it. In theory it should have worked. Andrew Mar 25, 2015 at 16:35
  • 2
    $("#anchorID")[0].click(); to use this solution with jquery.
    – theBell
    Sep 22, 2016 at 10:34
  • 3
    Lol theBell, It's not jQuery if you are accessing directly DOM Element [0] Jul 29, 2019 at 21:40
  • This is the best solution. Works in safari too.
    – Vidhya
    Nov 24, 2021 at 13:41
13

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.

1
  • Triggers Chrome built-in popup blocker.
    – Volomike
    Jan 4, 2022 at 14:54
8

usage of location.href will replace current url with new url i.e link 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');
}
1

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.

2
  • Popup prevented by browser.
    – Millar248
    Jan 29, 2019 at 20:40
  • 1
    It means that you either have an adblock or something else that blocks it. The default behaviour should be working under normal conditions Nov 13, 2019 at 17:36
0

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');
2
  • Are the first and second line required? Won't it work without the reportDate and url variables?
    – Angshu31
    Jun 4, 2020 at 20:53
  • You can take out the parameter (reportDate), but the url var is important because it contains the call to the action method. Jun 7, 2020 at 21:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.