1

I use this simple code to detect if adblock is active

<script>
(function(){
 var test = document.createElement('div');
 test.innerHTML = '&nbsp;';
 test.className = 'adsbox';
  document.body.appendChild(test);
 window.setTimeout(function() {
  if (test.offsetHeight === 0) {
  alert("active");      
} else {
 alert("not active");
}
test.remove();
}, 100);
})();

This works fine so far.

Instead of display a alert message, I need a jquery popup. Therefore I changed the above code to:

<script>
(function(){
 var test = document.createElement('div');
 test.innerHTML = '&nbsp;';
 test.className = 'adsbox';
 document.body.appendChild(test);
 window.setTimeout(function() {
if (test.offsetHeight === 0) {
 showmessage();      
}
test.remove();
}, 100);
})();
</script>

Without this function

showmessage();   

works perfect.

When showmessage(); is inside this function, the popup doesnt come up. Instead the showmessage(); content is displayed on the website without css and not as a popup. I think this has something todo with the timeout function. But without this timeout, the detetection does not work in firefox.

Content from showmessage();

function showmessage(){
document.write("<div id=\"boxes\">");
document.write("  <div id=\"dialog\" class=\"window\">");
document.write("  <div style=\"display: none; opacity: 0.8;\" id=\"mask\">
<\/div>");
 document.write("<\/div></div>");

setTimeout(
function() {
    var id = '#dialog';

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect     
    $('#mask').fadeIn(500); 
    $('#mask').fadeTo("slow",0.9);  

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    //$(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);

    //transition effect
    $(id).fadeIn(1000);     

//if close button is clicked
$('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();

    $('#mask').hide();
    $('.window').hide();
});     

//if mask is clicked
/*$('#mask').click(function () {
    $(this).hide();
    $('.window').hide();
});     */

}, 200);
}

Thank you very much

labu77
  • 605
  • 1
  • 9
  • 30
  • You don't want to use `document.write` which overwrite all page's content if called once page is loaded. Basically, just set a `div` hidden by default and show it if user use any ads blocker – A. Wolff Sep 02 '16 at 09:12
  • It doestn matter aslong the popup comes up. showmessage() works fine. thank you – labu77 Sep 02 '16 at 09:14
  • `showmessage() works fine` No, it doesn't... It doesn't work as you expect it to work. See again th reason here: http://stackoverflow.com/questions/19941866/document-write-overwriting-the-document – A. Wolff Sep 02 '16 at 09:20
  • Ok, thank you, I see. – labu77 Sep 02 '16 at 09:27

1 Answers1

0

Use the prepend method to body and then set the popup HTML string into the method . :

function showmessage(){
        var jQueryPopupHTML = "<div id='boxes'><div id='dialog' class='window'> <div style='display: none;  opacity: 0.8;' id='mask'></div></div></div>"
        $( "body" ).prepend(jQueryPopupHTML);

    setTimeout(
    function() {
        var id = '#dialog';

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(500); 
        $('#mask').fadeTo("slow",0.9);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        //$(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(1000);     

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    /*$('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });     */

    }, 200);
    }
  • Thank you very much. Works perfect. Would you please explain me in short my error? Also your code has no fade in anymore. Is it possible to add this too? – labu77 Sep 02 '16 at 09:27
  • After the page has finished loading, `document.write` cannot add more content to the page - instead it completely overwrites the existing content. Instead, you should use methods like [`document.createElement`](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement) and [`node.appendChild`](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild) to add new content to the page (or jQuery's [`append`](http://api.jquery.com/append/)/[`prepend`](http://api.jquery.com/prepend/)). This also gives you control over *where* the content is added to the DOM tree. – Jim O'Brien Sep 02 '16 at 09:31
  • tx for the explantion. I understand. Do you know why the fade in effect is not working anymore? – labu77 Sep 02 '16 at 09:37
  • ok found it. $(jQueryPopupHTML).hide().prependTo("body").fadeIn("slow"); – labu77 Sep 02 '16 at 09:56