Making a window pop under in chrome

前端 未结 8 1370
攒了一身酷
攒了一身酷 2020-12-03 05:55

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

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 06:45

    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-

提交回复
热议问题