Open a new tab in the background?

前端 未结 5 1483
闹比i
闹比i 2020-11-22 10:07

Using javascript, I want to open a new page in a different tab, but remain focused on the current tab. I know I can do it like this:

open(\'http://example.co         


        
5条回答
  •  悲&欢浪女
    2020-11-22 10:16

    UPDATE: By version 41 of Google Chrome, initMouseEvent seemed to have a changed behavior.

    this can be done by simulating ctrl + click (or any other key/event combinations that open a background tab) on a dynamically generated a element with its href attribute set to the desired url

    In action: fiddle

    function openNewBackgroundTab(){
        var a = document.createElement("a");
        a.href = "http://www.google.com/";
        var evt = document.createEvent("MouseEvents");
        //the tenth parameter of initMouseEvent sets ctrl key
        evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                                    true, false, false, false, 0, null);
        a.dispatchEvent(evt);
    }
    

    tested only on chrome

提交回复
热议问题