【发布时间】:2017-02-06 11:13:26
【问题描述】:
我想在 chrome 中的 new process/context 中打开新窗口,(我不确定 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);
})
}
【问题讨论】:
-
ETag 与此有什么关系?是的,人为触发的链接点击也可以被弹出窗口拦截器捕获 - 这是一件好事,因为否则douchevertisers也会使用这种技术。
-
@CBroe - 抱歉我的错误这与 Etag 无关...有一种方法可以知道这一点?我只是想从我的代码中知道我可以告诉用户禁用它,这是内部应用程序...\
-
@CBroe - 除了向用户提供一些信息外,我不想做任何事情,因此当用户单击打开窗口时我需要以某种方式知道这一点......
-
那你为什么不想使用window.open呢?由于这是一个方法调用,它有一个可以检查的返回值。单击链接没有任何返回值,因此使用此方法进行可比较的检查会更加复杂(如果可能的话)。
-
@CBroe - 谢谢,所以有办法使用 window open 在新进程中打开它?
标签: javascript google-chrome chromium referer popup-blocker