Skip to content

Instantly share code, notes, and snippets.

@formigone
Last active August 29, 2015 14:23
Show Gist options
  • Save formigone/739c34d57009a137ef96 to your computer and use it in GitHub Desktop.
Save formigone/739c34d57009a137ef96 to your computer and use it in GitHub Desktop.
Detect if ad blockers are present
/**
* Attempt to load a script from a popular ad network. Ad blockers will intercept the HTTP request.
*
* @param {string} url
* @return {Promise}
*/
function detectAdBlockerAsync(url){
var def = $.Deferred();
var script = document.createElement('script');
script.onload = function(){
$(script).remove();
def.resolve();
}
script.onerror = function(){
$(script).remove();
def.reject();
};
script.src = url;
document.body.appendChild(script);
return def.promise();
}
detectAdBlockerAsync('http://ads.pubmatic.com/AdServer/js/gshowad.js')
.then(function(){
document.body.style.background = '#0c0';
})
.fail(function(){
document.body.style.background = '#c00';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment