Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
HTML5 Ad Blocker detect code — Gideros Forum

HTML5 Ad Blocker detect code

SinisterSoftSinisterSoft Maintainer
edited June 2020 in Code snippets
HTML5 games don't work properly if the user has an Ad Blocker enabled. Here is a way to display a message if the user has one...

First, make a file called either 'ads.js' or 'dfp.js', I used the latter. Put this code in it...
var e=document.createElement('div');
e.id='BL0CK3R7357';
e.style.display='none';
document.body.appendChild(e);
The file should go in the root of your website, if you put it in a sub folder then the ad blocker may not detect it properly.

You can change the 'BL0CK3R7357' text to something else if you like, but remember to change it in the other code I'm about to mention too.

In your game's web page put this:
<div id="blocker">Sorry, the game can't load properly if you are using an ad blocker.<br>Please disable your ad blocker to play the game.</div>
Again, you can change 'blocker' to something else as long as you change it in the rest of the code.

Before the end of your webpage, just before the body ends, put this:


(I had to use an image as the forum couldn't display the code)

If you used 'ads.js' rather than 'dfp.js' then change the text - the same goes for if you changed 'BL0CK3R7357' or 'blocker'. Don't put the full domain name in the link to the .js file, if you do then the ad blocker may not detect the text properly on your page - just write '/ads.js' or '/dfp.js'.

In your stylesheet, add this:
#blocker {
display: none;
margin-bottom: 30px;
padding: 20px 10px;
background: #D30000;
text-align: center;
font-weight: bold;
color: #fff;
border-radius: 5px;
}
Change the #blocker if you changed the 'blocker' text above. Here you can set the colours and style of the message to suit your page.

Now test it. Here is one I made earlier: https://deluxepixel.com/zombierocks

Likes: oleg

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+1 / -0 )Share on Facebook

Comments

Sign In or Register to comment.