Detecting Ad-Blocker using JavaScript Tutorial

In this tutorial, you can learn how to detect the Ad-Blocker using JavaScript. The tutorial aims to provide students and beginners with a reference for learning to disallow users or visitors to explore the different pages and contents of a certain website. Here, I will be providing a simple web page script that demonstrates the Ad-Blocker detection.

What is an Ad-Blocker?

Ad-blocker is a software, program, or browser extension that has the capability to block or alter the advertisement block or field of a website. This program is designed to prevent the website to display any advertisement to the client side. Moreover, ad blockers prevent the loading of advertising by checking website elements against extensive blacklists.

How to Detect Ad-Blocker using JavaScript?

Detecting an Ad-Blocker when browsing the website can be achieved easily using some short line of JavaScript codes. Most Ad-blockers alter the Ad fields or elements by adding a display:none CSS property to the parent of the element to prevent showing it on the page or to stop the ads to be loaded.

Here are the following steps you must know to detect Ad-Blocker:

  1. First, you must identify the parent elements of the Advertisement blocks that are available on your site.
  2. Next, check if one of the Advertisement Blocks element or parent element display style is set to none.
  3. Then, execute the function or script you want to run if Ad-Blocker is detected.

Check out the sample web page scripts that I created and provided below to have a better idea for identifying or detecting if there's an Ad-blocker on the client side that blocks or prevents your site's advertisement fields to display or to be shown.

Sample Web Page

The scripts below result or outputs a simple web page that has the capability to detect Ad-blockers when browsing the page. The web page contains simple and static page content and shows a popup message for preventing the users to explore more on the website if the visitor is using an Ad-Blocker.

HTML

Here's the HTML file script named index.html that contains the page elements of the sample web page.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>JS - Detect AdBlocker</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  10. <link rel="stylesheet" href="style.css">
  11. </head>
  12. <!-- AdBlocker Detected Message Popup -->
  13. <div id="add-blocker-popup">
  14. <div class="add-blocker-dialog">
  15. <div class="ads-blocked-popup-img"><img src="./adblock.png" alt="AdBlock Image"></div>
  16. <div class="ads-blocked-popup-title">It looks like you're using an Ad-Blocker!</div>
  17. <div class="ads-blocked-popup-description">Kindly disable your Ad-Blocker on this site to continue exploring and reading the pages and contents of this website. Thank You!</div>
  18. </div>
  19. </div>
  20. <!-- AdBlocker Detected Message Popup -->
  21. <div id="wrapper">
  22. <div class="page-title">Detect AdBlocker and Display Message using JavaScript</div>
  23. <hr style="width:25px">
  24. <div id="content-wrapper">
  25. A popup message will be shown when AdBlocker is enabled and detected.
  26. </div>
  27. </div>
  28. <script src="script.js"></script>
  29. </body>
  30. </html>

Stylesheet

Here's the CSS file script named style.css that contains the codes for the design of the web page layout, contents, and popup message. This file is loaded in the index page file script.

  1. @import url('https://fonts.googleapis.com/css2?family=Rubik&display=swap');
  2. @import url('https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet');
  3. :root{
  4. --secular-font: 'Secular One', sans-serif;
  5. --satisfy-font: 'Satisfy', cursive;
  6. }
  7. *{
  8. box-sizing: border-box;
  9. }
  10. body *{
  11. font-family: 'Rubik', sans-serif;
  12. }
  13. /**
  14. Page Design
  15. */
  16. body,
  17. html{
  18. height: 100%;
  19. width: 100%;
  20. margin: 0;
  21. padding: 0;
  22. }
  23. body{
  24. background-color: #93C6E7;
  25. }
  26. /* Page Wrapper */
  27. #wrapper{
  28. width: 100%;
  29. height: 100%;
  30. display: flex;
  31. flex-direction: column;
  32. align-items: center;
  33. justify-content: center;
  34. }
  35. .page-title{
  36. font-size: 35px;
  37. font-family: var(--secular-font);
  38. letter-spacing: 2px;
  39. text-align: center;
  40. color:#fff;
  41. text-shadow: 0px 0px 5px #7070709c;
  42. }
  43.  
  44. #content-wrapper{
  45. width: 100%;
  46. padding: 3em 5em;
  47. text-align: center;
  48. color:#fff;
  49. font-size: 20px;
  50. }
  51.  
  52. /*AdBlock Detection Message Popup*/
  53. #add-blocker-popup{
  54. position: fixed;
  55. top:0;
  56. left:0;
  57. width: 100%;
  58. height: 100%;
  59. display: none;
  60. opacity: 0;
  61. flex-direction: column;
  62. align-items: center;
  63. justify-content: center;
  64. overflow: auto;
  65. backdrop-filter: brightness(0.5);
  66. transition:all .3s ease-in-out;
  67. transition-delay:.5s ;
  68. }
  69.  
  70.  
  71. #add-blocker-popup .add-blocker-dialog{
  72. width: 555px;
  73. min-height: 380px;
  74. display: flex;
  75. flex-direction: column;
  76. align-items: center;
  77. justify-content: center;
  78. background-color: #fff;
  79. padding: 15px;
  80. border: 1px solid #bebebe;
  81. border-radius: 10px;
  82. box-shadow: 1px 1px 8px #b7b2b2eb;
  83. transform: translateY(-1000px);
  84. }
  85.  
  86. @media (max-width: 555px){
  87. #add-blocker-popup .add-blocker-dialog{
  88. width:90%;
  89. min-height:80%;
  90. }
  91. }
  92.  
  93. /* Display Popup */
  94. #add-blocker-popup.show{
  95. transition-delay:0s ;
  96. display: flex;
  97. animation: modal-fade .3s ease-in-out forwards;
  98. }
  99. #add-blocker-popup.show .add-blocker-dialog{
  100. animation: modal-slide-down .3s ease-in-out forwards;
  101. }
  102. @keyframes modal-fade {
  103. 0%{
  104. opacity: 0;
  105. }
  106. 100%{
  107. opacity: 1;
  108. }
  109. }
  110. @keyframes modal-slide-down {
  111. 0%{
  112. transform: translateY(-1000px);
  113. }
  114. 100%{
  115. transform: translateY(0px);
  116. }
  117. }
  118. /* Popup message content*/
  119. .ads-blocked-popup-img {
  120. width: 150px;
  121. height: 150px;
  122. overflow: hidden;
  123. }
  124. .ads-blocked-popup-img>img {
  125. width: 100%;
  126. height: 100%;
  127. object-fit: fill;
  128. object-position: center center;
  129. }
  130. .ads-blocked-popup-title{
  131. font-size: 35px;
  132. font-weight: bolder;
  133. text-align: center;
  134. padding-bottom: 1em;
  135. }
  136. .ads-blocked-popup-description{
  137. text-align: center;
  138. color:#252525;
  139. }

JavaScript

lastly, here is the JavaScript file script named script.js that contains the codes for detecting the Ad-Blocker when browsing the website. This file is also included or loaded in the index page file script.

  1. /** AdBlock Popup Message Selector */
  2. const AdBlockMessage = document.getElementById('add-blocker-popup')
  3. /** Page Wrapper Selector */
  4. const pageWrapper = document.getElementById('wrapper')
  5.  
  6.  
  7. /* Function for Showing AdBlock Popup Message */
  8. const showAdBlockMessage = () => {
  9. if(!AdBlockMessage.classList.contains('show'))
  10. AdBlockMessage.classList.add('show');
  11. }
  12.  
  13. /** Sample Ad field */
  14. var AdEl = document.createElement('ins')
  15. AdEl.classList.add('adsbygoogle')
  16. AdEl.innerText = `Sample Ad Field`;
  17. pageWrapper.appendChild(AdEl)
  18.  
  19. /** Trigger AdBlocked Popup Message */
  20. /** Loop All the Adblock elements if your ad fields elements are multiple */
  21. const isAdBlocked = window.getComputedStyle(AdEl).getPropertyValue('display')
  22. if(isAdBlocked == 'none'){
  23. showAdBlockMessage()
  24. }
  25.  

Snapshots

Here are the web page result snapshots using the provided scripts above:

Ad-Blocker is Enabled on the Browser

Detecting Ad-Blocker using JavaScript

Ad-Blocker is Disabled on the Browser

Detecting Ad-Blocker using JavaScript

DEMO

Detecting Ad-Blocker using JavaScript

There you go! I have also provided the complete source code zip file on this site and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify the source code the way you wanted.

That's it! I hope this Detecting Ad-Blocker using JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment