我使用此代码来检测广告拦截程序Ghosteri:
<script>
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState === 4 && request.status === 200 ) {
console.log('No blocker');
}
else if(request.readyState === 4 && request.status === 0){
window.location.href = 'http://';
}
};
request.open("GET","http://xx.net/pop.js");
request.send();
</script>
在Chrome上运行良好,但在Firefox上没有。
一些想法?
您应该看到readyState更改时发生了什么(也就是说,如果正在更改)。请注意readState和状态。在此基础上,您可以添加更多的函数以用于不同的场景。
request.onreadystatechange = function(){
console.log(this);
}
您只定义了readyState为4且状态为200或0时的事件。还有很多其他状态可以返回。确保你为所有这些做好了准备。
我用这种方式解决了:
<script>
$.ajax({url:"http://xx/pop.js",
error: function() {
window.location.href = 'http://';
}});
</script>
如果Ghostery屏蔽了我的弹出窗口的url,则会显示带有"检测到AdBlock"的图像。
这在Firefox+Ghostery上非常适用。