在后台打开一个新标签页? [英] Open a new tab in the background?

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

问题描述

使用 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.

推荐答案

更新:到 Google Chrome 41 版, initMouseEvent 似乎有改变的行为,感谢@Daniel Andersson

这可以通过在动态生成的 a 上模拟 ctrl + click(或任何其他打开背景选项卡的键/事件组合)来完成code> 元素,其 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

实际操作: 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);
}

仅在 chrome 上测试

tested only on chrome

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

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