How to open link in new windows by _blank and in background?

后端 未结 2 371
一生所求
一生所求 2021-01-07 14:20
相关标签:
2条回答
  • 2021-01-07 14:29

    After you open the new window (we'll call it newWindow), do this:

    newWindow.blur();
    window.focus();
    

    This is usually called a "pop-under." Opening the window won't usually work unless it's initiated by a user action like a mouse click, due to pop-up blockers built into most web browsers.

    Just remember to get a handle to your new window when you open it.

    var newWindow = window.open(...);
    

    Also, if your pop-under is loading content from another domain, you'll need to do the blur/focus thing before you send the window to its destination, otherwise you'll run into the same-origin problem.


    HTML:

    <a href="xxx" target="_blank" onclick="return popUnder(this);">
    

    JS:

    function popUnder(node) {
        var newWindow = window.open("about:blank", node.target, "width=500,height=500");
        newWindow.blur();
        window.focus();
        newWindow.location.href = node.href;
        return false;
    }
    
    0 讨论(0)
  • 2021-01-07 14:38

    This is typically a browser preference. But if you can somehow get a hold to the other window with javascript (maybe by hooking up the onclick event and open the link with window.open you could do the typical pop-under trick:

    otherWindow.blur();
    window.focus();
    
    0 讨论(0)
提交回复
热议问题