505

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?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
iamjonesy
  • 24,732
  • 40
  • 139
  • 206

7 Answers7

1242
window.open(
  'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
  '_blank' // <- This is what makes it open in a new window.
);
alex
  • 479,566
  • 201
  • 878
  • 984
  • 117
    Worth to mention: Whether a new tab or window is created, is decided by the **browser** (setting). – jAndy Feb 28 '11 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 '11 at 17:51
  • 1
    Though late but useful, Someone asked if the `'_blank'` is default text or must be written that way or it does have any impact on the entire `window.open` (???)... Just for the records, the `_blank` can be `textString`. You could even write it as `my_newWindow` or whatever. Provided that: **The is no blank spaces in between the characters** also note that **this is NOT going to be the title*** of the new. window. – ErickBest Jul 23 '13 at 03:55
  • 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](http://jsfiddle.net/7kGnM/). – alex Jul 24 '13 at 09:39
  • Not really. Just commented out the 'if` statement in your fiddle. Both buttons work like VOODOO!. Try it and VOILA! `_random_string` works as well as `_blank`. In fact one of theAdvantages of this method is that the user has brand NewWindows for different purposes i.e: `my_SPORTS_Window`, my_NEWS_Window,`my_EMAIL_Window`...etc, & when S/he clicks on a Link that deals withSports, it'll goDirectly to `my_SPORTS_Window` and open there. How cool is that?, Please read thisDO NOT use target="_blank" by MDN. https://developer.mozilla.org/en-US/docs/Web/API/window.open#Do_not_use_target.3D.22_blank.22 – ErickBest Jul 25 '13 at 14:04
  • @ErickBest The `if` had nothing to do with it (it was used to ensure the clicks were from the `button` elements only). You're basically iterating what I mentioned in my comment. – alex Jul 25 '13 at 21:20
  • @Alex.. I get your point bro. However, The `If` disables the use of other custom strings. Uncomment it and try something like `myNewTab`, or `my_NewTab` or `myNewTab_`... --The button goes dormant--. The reason why I mentioned you should remove the `if` is that the `if` obliges strings to start with an `_` (underscore) as in, it being the first char of the string. `//Removing it === BOOM!` any string will open a brand new Tab or Window depending on the browsers' specifics and the window can be reused severally if necessary... If that's what you also mean, then we are on the same boat. – ErickBest Jul 26 '13 at 04:17
  • 1
    What is `type=individual`? Neither this nor the `_blank` part are necessary (any more). The `window.open` command opens a new browser window or tab by default. – erdomester Jan 11 '14 at 13:50
  • 4
    This just triggers a pop up block notification in modern browsers, doesn't simulate a `_blank` anchor click at all. – Nathan Hornby Oct 01 '14 at 12:46
  • 34
    popup prevented by browser. – Jitendra Pancholi Mar 17 '15 at 13:29
49

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)

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • 2
    Triggers Chrome's built-in popup blocker in the latest Chrome here on Jan 4, 2022. – Volomike Jan 04 '22 at 14:54
  • @Volomike In my chrome version is 96.0.4664.110 (Official Build) (arm64) (MacOs) - example works. What is you chrome version? – Kamil Kiełczewski Jan 04 '22 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. https://www.screencast.com/t/b6wA1Vay – Volomike Jan 04 '22 at 15:20
  • Perhaps you enabled popups from jsfiddle.net. That might be why you don't see the popup blocking. – Volomike Jan 04 '22 at 15:41
  • 1
    This solution is better than `window.open` since it can work in safari – hatched Jul 05 '22 at 05: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();
аlex
  • 5,426
  • 1
  • 29
  • 38
andrew field
  • 407
  • 4
  • 2
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.

Musa
  • 96,336
  • 17
  • 118
  • 137
user616639
  • 223
  • 2
  • 5
  • 9
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');
}
Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37
user3644678
  • 99
  • 1
  • 1
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.

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');
Primoshenko
  • 140
  • 1
  • 9
  • Are the first and second line required? Won't it work without the `reportDate` and `url` variables? – Angshu31 Jun 04 '20 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. – Primoshenko Jun 07 '20 at 21:39