javascript 防止 window.open 聚焦
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6897430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent window.open from focusing
提问by Nicu Surdu
I want to open a page in a new tab in Google Chrome with window.open(), but I don't want that window to gain focus after it's opened, but to stay in the background.
我想使用 window.open() 在 Google Chrome 的新标签页中打开一个页面,但我不希望该窗口在打开后获得焦点,而是留在后台。
Is this possible? It only has to work on Google Chrome. It can also use the Google Chrome extension API.
这可能吗?它只需要在谷歌浏览器上工作。它还可以使用 Google Chrome 扩展 API。
Thanks
谢谢
采纳答案by serg
The proper way would be to use extension API:
正确的方法是使用扩展 API:
chrome.tabs.create({url: "http://...", selected: false});
Code should be placed in a background page. If you need it inside a content script you can pass a message to a background page, like so:
代码应该放在后台页面中。如果您在内容脚本中需要它,您可以将消息传递到后台页面,如下所示:
//content script
chrome.runtime.sendMessage({link: link});
//background page
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if(message.link) {
chrome.tabs.create({url: link, selected: false});
}
});
回答by Amit
window.open(url, name, features);
window.focus();
You will see the new window for a short moment though.
不过,您会在短时间内看到新窗口。
回答by Talha Ahmed Khan
There is a way out in all the browser
所有浏览器都有出路
function openURL(url, opt){
if (opt == 0){ // current window
window.location = url;
}else if (opt == 1){ // new window
window.open(url);
}else if (opt == 2){ // background window
window.open(url); self.focus();
}
}
so By using this you can do anything you want.
所以通过使用它,你可以做任何你想做的事情。
openURL( "http://www.google.com", 0 ) --> open in same window
openURL( "http://www.google.com", 1 ) --> open in new window
openURL( "http://www.google.com", 2 ) --> open in new window but in background.
回答by Jouhny_Jo
Yes you can do that just use:
是的,您可以这样做,只需使用:
var myWindow = window.open(url,name,features);
myWindow.blur();
回答by Dhawal M
Try this: (checked on chrome only)
试试这个:(仅在 chrome 上检查)
var myWindow = window.open(url,name,features);
window.open().close();
回答by hungryMind
Activate parent window from child or activate self once child is opened.
从子窗口激活父窗口或在子窗口打开后激活自身。