Last active
August 29, 2015 14:23
-
-
Save formigone/739c34d57009a137ef96 to your computer and use it in GitHub Desktop.
Detect if ad blockers are present
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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