admin管理员组文章数量:1425758
My aim is to detect the ad-blocker. I found couple of good examples like FuckAdBlock.
When the ad service call is blocked by the ad-blocked we get the error "err_blocked_by_client".
I want to handle this error, in the following way :
var xhr = new XMLHttpRequest();
try
{
xhr.open("GET",".js", false);
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText)
} else {
console.log("Error", xhr.statusText);
}
}
};
xhr.send();
}
catch(error)
{
console.log("ConsoleLog \n " + JSON.stringify(xhr));
console.log("Error Catched" + error);
}
But in this case i am not able to identify the error reason on catch block.
Please let me know the better option to handle this error or my mistake in this code.
Thanks
My aim is to detect the ad-blocker. I found couple of good examples like FuckAdBlock.
When the ad service call is blocked by the ad-blocked we get the error "err_blocked_by_client".
I want to handle this error, in the following way :
var xhr = new XMLHttpRequest();
try
{
xhr.open("GET","http://static.adzerk/ados.js", false);
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText)
} else {
console.log("Error", xhr.statusText);
}
}
};
xhr.send();
}
catch(error)
{
console.log("ConsoleLog \n " + JSON.stringify(xhr));
console.log("Error Catched" + error);
}
But in this case i am not able to identify the error reason on catch block.
Please let me know the better option to handle this error or my mistake in this code.
Thanks
Share Improve this question edited Dec 3, 2014 at 17:24 Manish Patwari asked Dec 3, 2014 at 17:12 Manish PatwariManish Patwari 5686 silver badges21 bronze badges1 Answer
Reset to default 2You need to have the xhr
variable outside of your try. It fails on JSON.stringify(xhr)
- because xhr
is out of scope. You need to use the onerror error handler to handle the async XMLHttpRequest. You also need to remove the oEvent parameter from the function passed to onreadystatechange. See below:
var xhr = new XMLHttpRequest();
try
{
xhr.open("GET","http://static.adzerk/ados.js", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText)
} else {
console.log("Error", xhr.statusText);
}
}
};
xhr.onerror = function(e) {
console.log("Error Catched" + e.error);
};
xhr.send();
}
catch(error)
{
console.log("ConsoleLog \n " + JSON.stringify(xhr));
console.log("Error Catched" + error);
}
本文标签: jqueryHow to handle the errblockedbyclient using JavascriptStack Overflow
版权声明:本文标题:jquery - How to handle the err_blocked_by_client using Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745425979a2658114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论