Skip to content

Instantly share code, notes, and snippets.

@AnjanaMadu
Created May 23, 2022 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnjanaMadu/16e426fc01ec9b18da5b3530d3539777 to your computer and use it in GitHub Desktop.
Save AnjanaMadu/16e426fc01ec9b18da5b3530d3539777 to your computer and use it in GitHub Desktop.
A script or code can detect whether the client uses or enable the ad blocker.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$.ajax({
url: 'https://static.doubleclick.net/instream/ad_status.js',
success: function( data ) {
console.log("Ads loaded");
},
error: function( data ) {
console.log("You're using an ad blocker.");
document.body.innerHTML = '<center><br><h1>Turn off your Ads Blocker and refresh the page!</h1></center>';
}
});
</script>
@SpEcHiDe
Copy link

don't use jQuery for every small things 🥺😐

// create a XHR object
  var xhr = new XMLHttpRequest();
  // open the XHR object in asynchronous mode
  xhr.open("GET", "https://static.doubleclick.net/instream/ad_status.js", true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      // OK! we have a successful response.
     console.log("Ads loaded");
      // do something else with the response
    }
    else {
       console.log("You're using an ad blocker.");
    }
  };
  // make the request
  xhr.send();

@AnjanaMadu
Copy link
Author

don't use jQuery for every small things 🥺😐

// create a XHR object
  var xhr = new XMLHttpRequest();
  // open the XHR object in asynchronous mode
  xhr.open("GET", "https://static.doubleclick.net/instream/ad_status.js", true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      // OK! we have a successful response.
     console.log("Ads loaded");
      // do something else with the response
    }
    else {
       console.log("You're using an ad blocker.");
    }
  };
  // make the request
  xhr.send();

image
Did you tested this? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment