• We just launched and are currently in beta. Join us as we build and grow the community.

Detecting Ad-Blocker using JavaScript Tutorial

Far

Operational Security Analyst
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
107
Likes
197
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    JS - Detect AdBlocker</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  11. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  12. </

    head

    >
  13. <body

    >
  14. <!-- AdBlocker Detected Message Popup -->
  15. <div

    id

    =

    "add-blocker-popup"

    >
  16. <div

    class

    =

    "add-blocker-dialog"

    >
  17. <div

    class

    =

    "ads-blocked-popup-img"

    ><img

    src

    =

    "./adblock.png"

    alt

    =

    "AdBlock Image"

    ></

    div

    >
  18. <div

    class

    =

    "ads-blocked-popup-title"

    >

    It looks like you're using an Ad-Blocker!</

    div

    >
  19. <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

    >
  20. </

    div

    >
  21. </

    div

    >
  22. <!-- AdBlocker Detected Message Popup -->
  23. <div

    id

    =

    "wrapper"

    >
  24. <div

    class

    =

    "page-title"

    >

    Detect AdBlocker and Display Message using JavaScript</

    div

    >
  25. <hr

    style

    =

    "width:25px"

    >
  26. <div

    id

    =

    "content-wrapper"

    >
  27. A popup message will be shown when AdBlocker is enabled and detected.
  28. </

    div

    >
  29. </

    div

    >
  30. <script

    src

    =

    "script.js"

    ></

    script

    >
  31. </

    body

    >
  32. </

    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. #content-wrapper

    {
  44. width

    :

    100%

    ;
  45. padding

    :

    3em

    5em

    ;
  46. text-align

    :

    center

    ;
  47. color

    :

    #fff

    ;
  48. font-size

    :

    20px

    ;
  49. }

  50. /*AdBlock Detection Message Popup*/
  51. #add-blocker-popup

    {
  52. position

    :

    fixed

    ;
  53. top

    :

    0

    ;
  54. left

    :

    0

    ;
  55. width

    :

    100%

    ;
  56. height

    :

    100%

    ;
  57. display

    :

    none

    ;
  58. opacity

    :

    0

    ;
  59. flex-direction

    :

    column;
  60. align-items

    :

    center

    ;
  61. justify-content

    :

    center

    ;
  62. overflow

    :

    auto

    ;
  63. backdrop-filter:

    brightness(

    0.5

    )

    ;
  64. transition

    :

    all .3s

    ease-in-out;
  65. transition-delay

    :

    .5s

    ;
  66. }


  67. #add-blocker-popup

    .add-blocker-dialog{
  68. width

    :

    555px

    ;
  69. min-height

    :

    380px

    ;
  70. display

    :

    flex;
  71. flex-direction

    :

    column;
  72. align-items

    :

    center

    ;
  73. justify-content

    :

    center

    ;
  74. background-color

    :

    #fff

    ;
  75. padding

    :

    15px

    ;
  76. border

    :

    1px

    solid

    #bebebe

    ;
  77. border-radius

    :

    10px

    ;
  78. box-shadow

    :

    1px

    1px

    8px

    #b7b2b2eb

    ;
  79. transform

    :

    translateY(

    -1000px

    )

    ;
  80. }

  81. @media

    (

    max-width

    :

    555px

    )

    {
  82. #add-blocker-popup

    .add-blocker-dialog{
  83. width

    :

    90%

    ;
  84. min-height

    :

    80%

    ;
  85. }
  86. }

  87. /* Display Popup */
  88. #add-blocker-popup

    .show{
  89. transition-delay

    :

    0s

    ;
  90. display

    :

    flex;
  91. animation

    :

    modal-fade .3s

    ease-in-out forwards

    ;
  92. }
  93. #add-blocker-popup

    .show

    .add-blocker-dialog{
  94. animation

    :

    modal-slide-down .3s

    ease-in-out forwards

    ;
  95. }
  96. @keyframes

    modal-fade {
  97. 0%

    {
  98. opacity

    :

    0

    ;
  99. }
  100. 100%

    {
  101. opacity

    :

    1

    ;
  102. }
  103. }
  104. @keyframes

    modal-slide-down {
  105. 0%

    {
  106. transform

    :

    translateY(

    -1000px

    )

    ;
  107. }
  108. 100%

    {
  109. transform

    :

    translateY(

    0px

    )

    ;
  110. }
  111. }
  112. /* Popup message content*/
  113. .ads-blocked-popup-img

    {
  114. width

    :

    150px

    ;
  115. height

    :

    150px

    ;
  116. overflow

    :

    hidden

    ;
  117. }
  118. .ads-blocked-popup-img>

    img {
  119. width

    :

    100%

    ;
  120. height

    :

    100%

    ;
  121. object-fit

    :

    fill;
  122. object-position

    :

    center

    center

    ;
  123. }
  124. .ads-blocked-popup-title{
  125. font-size

    :

    35px

    ;
  126. font-weight

    :

    bolder

    ;
  127. text-align

    :

    center

    ;
  128. padding-bottom

    :

    1em

    ;
  129. }
  130. .ads-blocked-popup-description{
  131. text-align

    :

    center

    ;
  132. color

    :

    #252525

    ;
  133. }

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. /* Function for Showing AdBlock Popup Message */
  6. const

    showAdBlockMessage =

    (

    )

    =>

    {
  7. if

    (

    !

    AdBlockMessage.classList

    .contains

    (

    'show'

    )

    )
  8. AdBlockMessage.classList

    .add

    (

    'show'

    )

    ;
  9. }

  10. /** Sample Ad field */
  11. var

    AdEl =

    document.createElement

    (

    'ins'

    )
  12. AdEl.classList

    .add

    (

    'adsbygoogle'

    )
  13. AdEl.innerText

    =

    `Sample Ad Field`;
  14. pageWrapper.appendChild

    (

    AdEl)

  15. /** Trigger AdBlocked Popup Message */
  16. /** Loop All the Adblock elements if your ad fields elements are multiple */
  17. const

    isAdBlocked =

    window.getComputedStyle

    (

    AdEl)

    .getPropertyValue

    (

    'display'

    )
  18. if

    (

    isAdBlocked ==

    'none'

    )

    {
  19. showAdBlockMessage(

    )
  20. }


Snapshots

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

Ad-Blocker is Enabled on the Browser

Ad-Blocker is Disabled on the Browser

DEMO

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 =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,193

322,229

322,238

Top