如何在_blank和后台打开新窗口中的链接? [英] How to open link in new windows by _blank and in background?

查看:110
本文介绍了如何在_blank和后台打开新窗口中的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于普通链接< a href =xxxtarget =_ blank> ,该链接将在新窗口(或标签页)中打开。

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。打开窗口通常不会起作用,除非它是由鼠标点击等用户操作启动的,因为大多数Web浏览器都内置了弹出窗口阻止程序。

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(...);

此外,如果您的pop-under正在加载来自其他域的内容,则您需要执行模糊 / 焦点 之前将窗口发送到目的地,否则你将会遇到同源问题。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆