st-*_*ost 71 javascript google-chrome
使用javascript,我想在不同的选项卡中打开一个新页面,但仍然专注于当前选项卡.我知道我可以这样做:
open('http://example.com/');
focus();
Run Code Online (Sandbox Code Playgroud)
但是,当我在chrome中执行此操作时,它会在切换回当前选项卡之前将新选项卡闪烁一会儿.我想避免这种情况.
该应用程序是一个个人书签,因此它只需要在最新的Chrome中工作.
Amr*_*mro 99
更新:在谷歌浏览器版本41中, initMouseEvent
似乎有一个改变的行为.
这可以通过在动态生成的元素上模拟ctrl
+ click
(或打开背景选项卡的任何其他键/事件组合)a
并将其href
属性设置为所需的值来完成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);
}
Run Code Online (Sandbox Code Playgroud)
仅在铬上测试过
THX这个问题!在所有流行的浏览器上对我有用:
function openNewBackgroundTab(){
var a = document.createElement("a");
a.href = window.location.pathname;
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);
}
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(!is_chrome)
{
var url = window.location.pathname;
var win = window.open(url, '_blank');
} else {
openNewBackgroundTab();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
95459 次 |
最近记录: |