简体   繁体   中英

Age Verification Pop Up To Only Show Once

So I have an age verification pop up on a web-page, the pop up will show on any web page you land on. The issue I'm having is i cant figure out how to show it only on the users first page. I have looked into using cookies to achieve this, but I have not been able to get my head around how to adapt the current code that I have. I need a solution that is not going to require plugins if possible. Thank you in advance.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.8.2.js"></script>
<style type="text/css">
#overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
filter:alpha(opacity=70);
-moz-opacity:0.7;
-khtml-opacity: 0.7;
opacity: 0.7;
z-index: 100;
display: none;
}
.cnt223 a{
text-decoration: none;
}
.popup{
width: 100%;
margin: 0 auto;
display: none;
position: fixed;
z-index: 101;
}
.cnt223{
min-width: 600px;
width: 600px;
min-height: 150px;
margin: 100px auto;
background: #f3f3f3;
position: relative;
z-index: 103;
padding: 15px 35px;
border-radius: 5px;
box-shadow: 0 2px 5px #000;
}
.cnt223 p{
clear: both;
    color: #555555;
    /* text-align: justify; */
    font-size: 20px;
    font-family: sans-serif;
}
.cnt223 p a{
color: #d91900;
font-weight: bold;
}
.cnt223 .x{
float: right;
height: 35px;
left: 22px;
position: relative;
top: -25px;
width: 34px;
}
.cnt223 .x:hover{
cursor: pointer;
}
</style>
<script type='text/javascript'>
$(function(){
var overlay = $('<div id="overlay"></div>');
overlay.show();
overlay.appendTo(document.body);
$('.popup').show();
$('.close').click(function(){
$('.popup').hide();
overlay.appendTo(document.body).remove();
return false;
});




$('.x').click(function(){
$('.popup').hide();
overlay.appendTo(document.body).remove();
return false;
});
});
</script>
<script>
function goBack() {
    window.history.go(-2);
}
</script>
<div class='popup'>
<div class='cnt223'>
<h1>Important Notice</h1>
<p>
You must be over 18 to Purchase products on this website!
<br/>
<br/>
<a href='' class='close' style="color:green">I Am Over 18</a>
<a href='' class='goBack()' style="color:red">I Am Not</a>
</p>
</div>
</div>

I would recommend cookies for this, but you can also go with either:

LocalStorage or SessionStorage

Both are quite the same, but differs how long they will be stored in the browser. So you can store something like this:

sessionStorage.setItem("ageverified", "yes");

To check the value befor showing the overlay, you can get the value like this:

var ageverified = sessionStorage.getItem("ageverified");

Of course the validation of your age verification should be done on server side.

Using cookies is the right approach. You can set a simple cookie by using eg document.cookie = "verified=true"; that is set after age confirmation is done successfully.

On the beginning of each page, check if cookie is existent and if document.cookie == "verified=true"

Please note that any access restriction done with JavaScript can easily be bypassed.

You place a cookie on page load and on all pages you check if that cookie already exists. If cookie is already set, then skip popup.

JSFiddle: https://jsfiddle.net/tx9w4apf/

Replace your javascript with followign code:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
$(function(){
var cookie = getCookie("agepopshown");
if(cookie ===1)
{
var overlay = $('<div id="overlay"></div>');
overlay.show();
overlay.appendTo(document.body);
$('.popup').show();
$('.close').click(function(){
$('.popup').hide();
overlay.appendTo(document.body).remove();
return false;
});




$('.x').click(function(){
$('.popup').hide();
overlay.appendTo(document.body).remove();
return false;
});
}
setCookie("agepopshown", 1,  365 );

});

function goBack() {
    window.history.go(-2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM