ChatGPT解决这个技术问题 Extra ChatGPT

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

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?


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

Worth to mention: Whether a new tab or window is created, is decided by the browser (setting).
@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?
@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.
This just triggers a pop up block notification in modern browsers, doesn't simulate a _blank anchor click at all.
popup prevented by browser.
а
аlex dykyі

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();

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.
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
$("#anchorID")[0].click(); to use this solution with jquery.
Lol theBell, It's not jQuery if you are accessing directly DOM Element [0]
This is the best solution. Works in safari too.
K
Kamil Kiełczewski

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)


This solution. +++
Triggers Chrome's built-in popup blocker in the latest Chrome here on Jan 4, 2022.
@Volomike In my chrome version is 96.0.4664.110 (Official Build) (arm64) (MacOs) - example works. What is you chrome version?
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
Perhaps you enabled popups from jsfiddle.net. That might be why you don't see the popup blocking.
M
Musa

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.


Triggers Chrome built-in popup blocker.
u
user3644678

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


佚名

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.


Popup prevented by browser.
It means that you either have an adblock or something else that blocks it. The default behaviour should be working under normal conditions
P
Primoshenko

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

Are the first and second line required? Won't it work without the reportDate and url variables?
You can take out the parameter (reportDate), but the url var is important because it contains the call to the action method.