如何通过_blank 在新窗口和后台打开链接? [英] How to open link in new windows by _blank and in background?
问题描述
for a normal link <a href="xxx" target="_blank">
, the link will open at a new window(or tab).
我想知道是否有办法在后台打开它,并使当前窗口保持活动状态.
I like to know if there's a way to open it in background, and keep current window still active.
推荐答案
在您打开
新窗口(我们将其称为 newWindow
)后,执行以下操作:
After you open
the new window (we'll call it newWindow
), do this:
newWindow.blur();
window.focus();
这通常称为pop-under".由于大多数网络浏览器都内置了弹出窗口阻止程序,因此打开窗口通常不会起作用,除非它是由用户单击鼠标等操作启动的.
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.
只要记住在打开新窗口时获得它的句柄即可.
Just remember to get a handle to your new window when you open it.
var newWindow = window.open(...);
此外,如果您的弹出窗口正在加载来自另一个域的内容,您需要在 之前做blur
/focus
事情你将窗口发送到它的目的地,否则你会遇到同源问题.
Also, if your pop-under is loading content from another domain, you'll need to do the blur
/focus
thing before you send the window to its destination, otherwise you'll run into the same-origin problem.
HTML:
<a href="xxx" target="_blank" onclick="return popUnder(this);">
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;
}
这篇关于如何通过_blank 在新窗口和后台打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!