Making a window pop under in chrome

2020-01-29 07:37发布

I have a button that needs to open a new window as a popup (under the parent page). In IE/Firefox, it works fine, but in chrome the popup appears over (on top of) the parent window.

Please suggest a fix.

use case/eg: Well, for eg if you see kayak.com or any travel website, you have the ability to search on other websites too..I want to do something similar so need the pop under...

Code: I am using a window.open(.......).blur(), but for some reason it isnt working in chrome.

7条回答
我欲成王,谁敢阻挡
2楼-- · 2020-01-29 07:57

This is the fix you can use for Chrome (tested on lastest v.40 on 29/01/2015). This won't open a window popup but a new tab and keeps on main tab focused(no more keeps focus on main tab on chrome v.43>).

To avoid popup blocker, you need user interaction, use specifically mousedown or mouseup event, click will throw a popup blocker warning.

document.addEventListener("mousedown", tabUnder);

function tabUnder() {
    var a = document.createElement("a"),
        e = document.createEvent("MouseEvents");
    a.href = "http://testit.com"; //the URL of 'popup' tab
    e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
    a.dispatchEvent(e);
    document.removeEventListener("mousedown", tabUnder);
}

-jsFiddle-

查看更多
登录 后发表回答