{"id":1544,"date":"2016-02-08T22:44:48","date_gmt":"2016-02-08T22:44:48","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1544"},"modified":"2016-02-08T22:49:15","modified_gmt":"2016-02-08T22:49:15","slug":"how-to-detecting-ad-blockers-on-your-website-using-javascript","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2016\/02\/how-to-detecting-ad-blockers-on-your-website-using-javascript\/","title":{"rendered":"How to Detecting Ad Blockers on Your Website using JavaScript ?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/02\/ad_blocker_phpmind.png\" alt=\"ad_blocker_phpmind\" width=\"604\" height=\"272\" class=\"aligncenter size-full wp-image-1546\" srcset=\"https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/02\/ad_blocker_phpmind.png 604w, https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/02\/ad_blocker_phpmind-300x135.png 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><br \/>\nThis code snippet will display a short message to the user on every page load if ad blockers in place.<\/p>\n<p>It encourages readers to disable ad blocking from your site, which is as easy for them as a couple of clicks. <\/p>\n<pre lang=\"javascript\">\r\n<div class=\"afs_ads\">&nbsp;<\/div>\r\n<script>\r\n(function() {\r\n    var message = \"We've detected that you have an ad blocker enabled! Please enable it and help support our work!\";\r\n\r\n        \/\/ Define a function for showing the message.\r\n        \/\/ Set a timeout of 2 seconds to give adblocker\r\n        \/\/ a chance to do its thing\r\n        var tryMessage = function() {\r\n            setTimeout(function() {\r\n                if(!document.getElementsByClassName) return;\r\n                var ads = document.getElementsByClassName('afs_ads'),\r\n                    ad  = ads[ads.length - 1];\r\n\r\n                if(!ad\r\n                    || ad.innerHTML.length == 0\r\n                    || ad.clientHeight === 0) {\r\n                    alert(message);\r\n                    \/\/window.location.href = '[URL of the donate page. Remove the two slashes at the start of thsi line to enable.]';\r\n                } else {\r\n                    ad.style.display = 'none';\r\n                }\r\n\r\n            }, 2000);\r\n        }\r\n\r\n        \/* Attach a listener for page load ... then show the message *\/\r\n        if(window.addEventListener) {\r\n            window.addEventListener('load', tryMessage, false);\r\n        } else {\r\n            window.attachEvent('onload', tryMessage); \/\/IE\r\n        }\r\n})();\r\n<\/script>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code snippet will display a short message to the user on every page load if ad blockers in place. It encourages readers to disable ad blocking from your site, which is as easy for them as a couple of clicks. &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[27],"tags":[],"class_list":["post-1544","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1544"}],"version-history":[{"count":4,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1544\/revisions"}],"predecessor-version":[{"id":1549,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1544\/revisions\/1549"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}