Search code examples
javascriptjquerysimplemodal

Simple Modal | Age Verification on First visit | Close but no cookie


I have been trying to get SimpleModal Age Verification page to popup on the first visit to my site. Meaning if the previous site wasn't me display popup. The code below is what I have so far. Basically the issue is that I am getting the popup once in a while for no reason when I click a link. I can't find a rhyme or reason too it. I have had a lot of help from the people here with this code. I'll get a little further then a problem pops up, then i'll fix that and another one pops up. I hope I'm getting pretty close here. ;)

Thanks guys!

<div class="age" id="verify"> 
    <div><img src="/image/white.png"></img></div>
    <div id="noman">ARE YOU OVER 18?</div>
    <div> 
      <p> If not, leave now and we wont tell your mom.
        </br>  By continuing you agree you are 18 or older.
      </p>
    </div>
    <div id="YN">
      <a href="javascript:redirect(window.location.href);" id="old">Yes</a>
        &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="http://www.poo.com" rel="nofollow" id="young">No</a>
    </div>
</div>

<script>
$(document).ready(function(){
if ( document.referrer == null || document.referrer.indexOf(window.location.hostname) < 0 ) {
$("#verify").modal({opacity:85, position: ["20%",""], onOpen: function (dialog) {
    dialog.overlay.fadeIn('slow', function () {
        dialog.container.slideDown('slow', function () {
            dialog.data.fadeIn('slow');
            return false;
        });
    });
}});
}
});
</script>

<script type="text/javascript" >            
function redirect(url) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    } else {
        location.href = url;
    }
}
</script>

Solution

  • The problem is that you use document.referrer, but its use should be avoided.

    For example, I have disabled it using Firefox's about:config page.

    Or even if enabled, it doesn't work with window.open.

    Then, better use something like sessionStorage (or localStorage).