我想在 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");
虽然这正在工作,但有时新窗口/选项卡会被弹出窗口阻止程序阻止,我只想知道这一点并在内部通知用户新窗口被阻止并请启用它...我有哪个选项?
我的要求是这样的:
- 在新进程/上下文中打开窗口
- 如果弹出窗口阻止程序被阻止通知用户
怎么做?
更新
我需要它,因为当您单击以从现有窗口打开新窗口并打开第二个窗口并且您返回到第一个/源窗口并且您想做一些它被阻止的事情时!
为了模拟这一点,您可以创建这个简单的文件
<!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>
现在执行以下操作
- 运行程序(html 文件),这将打开 CNN 选项卡
- 检查 CNN 选项卡并在其上放置断点 (在源选项卡中,您可以找到 javascript,将其放在您选择的任何位置),直到它停止(您需要刷新页面,直到看到调试器停止...
- 现在回到第一个选项卡是两个按钮,你看到它被阻止了,你不能做任何事情,比如点击等等......
如何在不阻塞第一个/源选项卡的情况下处理打开新选项卡?
更新 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);
})
}