How to handle err_blocked_by_client using javascript?

My goal is to find an ad unit. I found a couple of good examples like FuckAdBlock .

When an ad service call is blocked by an ad unit, we get the error "err_blocked_by_client".

I want to handle this error as follows:

var xhr = new XMLHttpRequest(); try { xhr.open("GET","http://static.adzerk.net/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 cannot determine the cause of the error in the catch block.

Please let me know the best option to handle this error or my error in this code.

thanks

+5
source share
1 answer

You need to have the xhr variable outside of your attempt. It does not work on JSON.stringify(xhr) - because xhr out of scope. To handle asynchronous XMLHttpRequest, you must use the onerror error handler. 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.net/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); } 
+3
source

Source: https://habr.com/ru/post/1208273/


All Articles