对于普通链接 <a href="xxx" target="_blank">
, 链接将在新窗口(或标签页)打开。
我想知道是否有办法在后台打开它,并使当前窗口保持事件状态。
最佳答案
打开
新窗口(我们称它为newWindow
)后,执行以下操作:
newWindow.blur();
window.focus();
这通常称为“背投广告”。由于大多数 Web 浏览器内置的弹出窗口拦截器,打开窗口通常不会起作用,除非它是由用户操作(例如鼠标单击)启动的。
请记住在打开新窗口时获取新窗口的句柄。
var newWindow = window.open(...);
此外,如果您的背后弹出式广告正在从另一个域加载内容,您需要在 之前执行blur
/focus
操作你将窗口发送到它的目的地,否则你会遇到同源问题。
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;
}
关于javascript - 如何通过 _blank 和在后台在新窗口中打开链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9544519/