function blockAdblockUser() {
if ($('.myTestAd').filter(':visible').length == 0) {
// All are hidden, or "not visible", so:
// Redirect, show dialog, do something...
} else if ($('.myTestAd').filter(':hidden').length > 0) {
// Maybe a different error if only some are hidden?
// Redirect, show dialog, do something...
}
}
window.setTimeout(function(){
if(adsbygoogle instanceof Array) {
// adsbygoogle.js did not execute; probably blocked by an ad blocker
} else {
// adsbygoogle.js executed
}
}, 2000);
为了澄清,这里有一个AdSense异步广告代码的示例:
<!-- this can go anywhere -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- this is where the ads display -->
<ins class="adsbygoogle" ...></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
$.ajax({
url: "/scripts/advertisement.js", // this is just an empty js file
dataType: "script"
}).fail(function () {
// redirect or display message here
});
$('a').click(function(e){ // change $('a') into more specific selector
const openedWindow = window.open(this.href, '_blank');
// Check if browser tab was closed within 0.3 second (user can't, adblock does).
setTimeout(() => {
if (openedWindow.closed) {
alert('Adblock detected!');
}
}, 300);
e.preventDefault(); // return false if you like
});
<script src="/js/adsbygoogle.js"></script>
<script>
if(window.isAdsDisplayed === undefined ) {
// AdBlock is enabled. Show message or track custom data here
}
</script>
<script src="advertisement.js"></script>
<table id="tbl_ab_appeal" style="width: 900px; margin-left:auto; margin-right: auto; padding: 25px; background: #FCC; border: 1px solid #F66; visibility:collapse; border-collapse: collapse;">
<tr>
<td>We've detected that you're using an <strong>ad content blocking</strong> browser plug-in or feature. Ads provide a critical source of revenue to the continued operation of [This website name]. We ask that you disable ad blocking while on [This
website name] in the best interests of our community.</td>
</tr>
</table>
<script>
if (document.getElementById("tester") == undefined) adsBlocked();
function adsBlocked() {
document.getElementById("tbl_ab_appeal").style.visibility = "visible";
document.getElementById("tbl_ab_appeal").style.borderCollapse = "separate";
document.getElementById("tbl_ab_appeal").style.marginTop = "10px"
document.getElementById("tbl_ab_appeal").style.marginBottom = "10px"
}
</script>
<script onerror="adsBlocked()" src="//www.googletagservices.com/tag/js/gpt.js"></script>
setTimeout(function() {
var a = document.querySelector('.showads'),
b = a ? (a.offsetHeight ? false : true) : true;
console.log('ads blocked?', b)
}, 200); // don't too fast or will make the result wrong.
<div class="ads showads">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</div>
window.onload = function() {
setTimeout(function() {
var ad = document.querySelector("ins.adsbygoogle");
if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
console.log('You seem to blocking Google AdSense ads in your browser.');
}
}, 2000);
};
jQuery.getScript(
"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",
function() {
// Load your ad now
}).fail(function() {
// Google failed to load main script, do something now
});
function detectAdblock(callback) {
fetch('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', {
method: 'HEAD',
mode: 'no-cors',
}).then((response) => {
// If the request is redirected, then the ads are blocked.
callback(response.redirected)
}).catch(() => {
// If the request fails completely, then the ads are blocked.
callback(true)
})
}
detectAdblock((isAdblockerDetected) => {
console.log(`ads are ${isAdblockerDetected ? 'blocked' : 'not blocked'}`)
});