465

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
  • 29,742
  • 25
  • 114
  • 177
iamjonesy
  • 23,902
  • 39
  • 133
  • 206

7 Answers7

1151
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
  • 460,746
  • 196
  • 858
  • 974
  • 114
    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
  • 6
    @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
  • 2
    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
  • 30
    popup prevented by browser. – Jitendra Pancholi Mar 17 '15 at 13:29
37

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 dykyі
  • 4,877
  • 25
  • 38
andrew field
  • 387
  • 3
  • 2
33

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
  • 71,169
  • 26
  • 324
  • 295
12

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
  • 93,746
  • 17
  • 112
  • 129
user616639
  • 213
  • 2
  • 5
  • 9
0

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');
0

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'); }