11

我想在 chrome 的新进程/上下文中打开新窗口,(我不确定 window.open 是否可能,但在下面的例子中它可以工作)目前如果它是常规窗口,你可以用下面的例子检查并查看是否弹出窗口拦截器已启用

ar newWin = window.open(url);             

if(!newWin || newWin.closed || typeof newWin.closed=='undefined') 
{ 
     //POPUP BLOCKED
}

但我想在没有 window.open 的新进程中打开新窗口,如下所示

var prod = document.getElementById("myElement"); 
var aTag = document.createElement('a');
aTag.setAttribute('href',"http://cnn.com");
//THIS TWO LINES do the job
aTag.setAttribute('rel',"noreferrer");
aTag.setAttribute('target',"_blank");
prod.appendChild(aTag);
aTag.click();
prod.removeChild(aTag);

与此参考一起使用: http ://news.softpedia.com/news/Force-Google-Chrome-to-Open-Links-in-New-Processes-128962.shtml

从帖子到在新上下文中打开新选项卡,您应该使用:

  aTag.setAttribute('rel',"noreferrer");
  aTag.setAttribute('target',"_blank");

虽然这正在工作,但有时新窗口/选项卡会被弹出窗口阻止程序阻止,我只想知道这一点并在内部通知用户新窗口被阻止并请启用它...我有哪个选项?

我的要求是这样的:

  1. 在新进程/上下文中打开窗口
  2. 如果弹出窗口阻止程序被阻止通知用户

怎么做?

更新

我需要它,因为当您单击以从现有窗口打开新窗口并打开第二个窗口并且您返回到第一个/源窗口并且您想做一些它被阻止的事情时!

为了模拟这一点,您可以创建这个简单的文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BlockTAB</title>
</head>

<br/>
<br/>
<button onclick="windowOpen('http://cnn.com')">window open native</button>
<br/>
<br/>
<button onclick="windowOpen('http://google.com')">sample</button>

<script>
    function windowOpen(url){
        window.open(url);
    }

</script>
</html>

现在执行以下操作

  1. 运行程序(html 文件),这将打开 CNN 选项卡
  2. 检查 CNN 选项卡并在其上放置断点 (在源选项卡中,您可以找到 javascript,将其放在您选择的任何位置),直到它停止(您需要刷新页面,直到看到调试器停止...
  3. 现在回到第一个选项卡是两个按钮,你看到它被阻止了,你不能做任何事情,比如点击等等......

如何在不阻塞第一个/源选项卡的情况下处理打开新选项卡?

更新 2

如果代码没有发生这种情况,有一种方法可以模拟弹出窗口阻止程序

aTag.setAttribute('rel',"noreferrer"); aTag.setAttribute('target',"_blank");

在前面的示例中添加以下代码

<script src="https://cdn.jsdelivr.net/bluebird/3.4.5/bluebird.js"></script>
<br/>
<br/>
<button onclick="EnabledPPB('http://cnn.com')">Blocker</button>

function delayFN(url) {
    Promise.delay(500)
        .then(function() {
            var newWin = window.open(url);
        })
}

function EnabledPPB(url) {
    Promise.delay(100)
        .then(function() {
            delayFN(url);
        })

}
4

1 回答 1

3

不太确定您是否要确保打开一个新窗口或新进程,所以也许我会同时回答。

如果你的字面意思是你想确保 Chrome 启动一个新进程,那么在 Javascript 中没有办法做到这一点,甚至你用window.open. 但是,由于 Chrome 正在为每个选项卡打开一个新进程,因此只要您的用户使用 Chrome,就可以安全地假设您的消息处于一个新进程中。另一方面,检查您的用户正在使用的浏览器是可能的。尽管您的用户可能仍然伪造用户代理(即浏览器),但对于内部使用来说应该足够了。

更多关于 Chrome 使用新进程而不是每个选项卡的新线程的参考。

如果您想确保您的用户在新窗口中打开您的消息,带有window.open的选项是唯一的选项。window.open您可以通过添加事件监听器或简单地使用onclickHTML 中的属性来触发。没有办法单独使用 HTML,当您已经找到使用 javascript 的方法时,应该没有理由搜索答案?

于 2016-10-03T11:19:46.353 回答