6

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.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 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

Manish Patwari
  • 566
  • 6
  • 20

1 Answers1

2

You 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.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);
}
James
  • 13,571
  • 6
  • 61
  • 83
Donal
  • 31,121
  • 10
  • 63
  • 72
  • Sorry My mistake in writing the code in question. I have done the same way. But it is not working. No where in the log i am getting "err_blocked_by_client". – Manish Patwari Dec 03 '14 at 17:23
  • @ManishPatwari ok, I see the issue. You need to use the onerror handler - because it is an async call. – Donal Dec 03 '14 at 17:29
  • Please give me the code. I tried this : xhr.onerror =function(error) { console.log("on Error"); } ; but this also didn't work. – Manish Patwari Dec 03 '14 at 17:41
  • @ManishPatwari The onerror event should be registered before callin g send. I have edited the answer. – Donal Dec 03 '14 at 18:08
  • I tried this but still didn't get the success. Please find the screenshot https://www.transferbigfiles.com/aaf68cc7-04fb-48ba-9b23-3b4df9fa28b6/iAfouScrWw0xDZI-6JB0nw2 – Manish Patwari Dec 04 '14 at 04:59
  • @ManishPatwari ok, I have tested it and found the issue. It was the oEvent parameter. I removed it and got it working - well I was getting a Cross origin requests issue - because I was running the script locally. You will not get that issue if you are hosting the script on the same server. Try the script in my answer. – Donal Dec 04 '14 at 05:16
  • Hi Donal, I tried the way you said, But still i am not getting the error reason from xhr object. https://www.transferbigfiles.com/812f1c6c-7c50-48a6-a7cc-0b8929f08899/mC6DGCi9V2v1kx2GoNXNWg2 – Manish Patwari Dec 04 '14 at 05:36
  • @ManishPatwari that screen shot is showing that you are actually getting the error you were initially looking for - the err_blocked_by_client error - you just need to catch it. I noticed you did not have any catch blocks in your screen shot. Use the code from my answer. – Donal Dec 04 '14 at 05:41
  • First i tried with the catch block https://www.transferbigfiles.com/ee16e439-b544-4275-a8df-1bf262bc64b2/GSTuJ92khc5H-AURHAo0xA2 but i think we don't need catch block because our error is getting handled in the onreadystatechange function. So, i removed that and tried, but still no luck :( – Manish Patwari Dec 04 '14 at 06:21
  • If you want to test on your system - Install ad-blocker ( https://chrome.google.com/webstore/detail/adblock-plus/cfhdojbkjhnklbpkdaibdccddilifddb ) and open the stackoverflow console and run the code. You will get the output. – Manish Patwari Dec 04 '14 at 06:24
  • hello, did you ever got the right solution to this problem @Donal ? – Grzegorz Pawlik Apr 26 '16 at 10:09