在后台打开一个新选项卡? [英] Open a new tab in the background?

查看:110
本文介绍了在后台打开一个新选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用javascript,我想在不同的选项卡中打开一个新页面,但仍然关注当前选项卡。我知道我可以这样做:

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.com/');
focus();

然而,当我在chrome中执行此操作时,它将闪烁新选项卡一会儿,然后切换回当前标签。我想避免这种情况。

However, when I do this in chrome, it flashes the new tab for a moment before switching back to the current tab. I want to avoid this.

该应用程序是一个个人书签,因此它只能在最新的Chrome中运行。

The application is a personal bookmarklet, so it only has to work in the latest Chrome.

推荐答案

更新:在谷歌浏览器版本41中, initMouseEvent 似乎有改变的行为

这可以通过模拟 ctrl + 在动态生成的 a 元素上单击(或任何其他打开背景选项卡的键/事件组合),并使用 href 属性设置为所需的 url

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

实际操作: 小提琴

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

仅在chrome上测试

tested only on chrome

这篇关于在后台打开一个新选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆