Search code examples
javascriptadblockrocketscript

Script is displaying text on both occasions


I'm trying to display text when a user is using adblock; I am using the following script;

ads.js

<script>var canRunAds = true;</script>

index.php

<script data-rocketsrc="ads.js" type="text/rocketscript"></script>
<script type="text/rocketscript">
  if( window.canRunAds === undefined ){
    var x = "Adblock is enabled, Please disabled to continue.";
    document.write (x);
  }
</script>

However the problem I've been having is that the text is being displayed when the variable is defined and when its not defined.


Solution

  • In ads.js, set window.canRunAds. You'll also need to use typeof to check for undefined.

    ads.js

    window.canRunAds = true;
    

    index.php

    <script src="/ads/ads.js"></script>
    <script>
      if (typeof window.canRunAds === 'undefined') {
        var x = "Adblock is enabled, Please disabled to continue.";
        document.write (x);
      }
    </script>