javascript 如何通过_blank 在新窗口和后台打开链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9544519/
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
How to open link in new windows by _blank and in background?
提问by Eric Yin
for a normal link <a href="xxx" target="_blank">
, the link will open at a new window(or tab).
对于普通链接<a href="xxx" target="_blank">
,链接将在新窗口(或选项卡)中打开。
I like to know if there's a way to open it in background, and keep current window still active.
我想知道是否有办法在后台打开它,并使当前窗口保持活动状态。
回答by Dagg Nabbit
After you open
the new window (we'll call it newWindow
), do this:
在您open
打开新窗口(我们称之为newWindow
)之后,执行以下操作:
newWindow.blur();
window.focus();
This is usually called a "pop-under." Opening the window won't usually work unless it's initiated by a user action like a mouse click, due to pop-up blockers built into most web browsers.
这通常称为“pop-under”。由于大多数 Web 浏览器中内置了弹出窗口阻止程序,因此打开窗口通常不会起作用,除非它是由用户操作(例如单击鼠标)启动的。
Just remember to get a handle to your new window when you open it.
只要记住在打开新窗口时获得它的句柄。
var newWindow = window.open(...);
Also, if your pop-under is loading content from another domain, you'll need to do the blur
/focus
thing beforeyou send the window to its destination, otherwise you'll run into the same-origin problem.
另外,如果您的弹出下是从另一个域加载的内容,你需要做的blur
/focus
事情之前,你发送的窗口,它的目的地,否则你会碰到同样的原产地问题。
HTML:
HTML:
<a href="xxx" target="_blank" onclick="return popUnder(this);">
JS:
JS:
function popUnder(node) {
var newWindow = window.open("about:blank", node.target, "width=500,height=500");
newWindow.blur();
window.focus();
newWindow.location.href = node.href;
return false;
}
回答by Andre Loker
This is typically a browser preference. But if you can somehow get a hold to the other window with javascript (maybe by hooking up the onclick event and open the link with window.open you could do the typical pop-under trick:
这通常是浏览器首选项。但是,如果您可以通过 javascript 以某种方式控制另一个窗口(也许通过连接 onclick 事件并使用 window.open 打开链接,您可以执行典型的 pop-under 技巧:
otherWindow.blur();
window.focus();