JavaScript On First Visit Agree to ToS

by Adrian Carriger

HTML

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div id="success" style="display:none" title="Welcome!">
    <p>This is the first time you have visited our site, to continue to the site: Click 'Ok' if you agree to our <b>ToS</b>: <a href="/tos.html">Here</a>
        <br>
        <br>Otherwise press the back button or close this window.</p>
</div>
        
<button style="display:none" id="remove-cookie">Remove cookie</button>

CSS

.ui-dialog-titlebar-close {
    visibility: hidden;
}
.ui-widget.success-dialog {
    font-family: Verdana, Arial, sans-serif;
    font-size: .8em;
}
.ui-widget-content.success-dialog {
    background: #F9F9F9;
    border: 1px solid #90d93f;
    color: #222222;
}
.ui-dialog.success-dialog {
    left: 0;
    outline: 0 none;
    padding: 0 !important;
    position: absolute;
    top: 0;
}
.ui-dialog.success-dialog .ui-dialog-content {
    background: none repeat scroll 0 0 transparent;
    border: 0 none;
    overflow: auto;
    position: relative;
    padding: 0 !important;
    margin: 0;
}
.ui-dialog.success-dialog .ui-widget-header {
    background: #b0de78;
    border: 0;
    color: #fff;
    font-weight: normal;
}
.ui-dialog.success-dialog .ui-dialog-titlebar {
    padding: 0.1em .5em;
    position: relative;
    font-size: 1em;
}
.ui-dialog .ui-dialog-buttonpane {
    text-align: center;
}
.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset {
    float: none;
}

JavaScript

function doit() {
    document.getElementById('uuid').innerHTML = generateUUID();
    var yes = getUrlVars()["agreed"];
    if (yes == "true") {
        setCookie('VisitDate', 1, 365);
    }
    if (yes != "true") {
        window.location = "/agree.htm";
    }
}

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
        vars[key] = value;
    });
    return vars;
}

function getCookie(NameOfCookie) {
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(NameOfCookie + "=");
        if (begin != -1) {
            begin += NameOfCookie.length + 1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}

function setCookie(NameOfCookie, value, expiredays) {
    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

    document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie(NameOfCookie) {
    if (getCookie(NameOfCookie)) {
        document.cookie = NameOfCookie + "=" +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function DoTheCookieStuff(LastChangeDate) {
    dt = new Date();
    year = dt.getYear();
    if (year <= 9) {
        year = "0" + year
    };
    month = dt.getMonth() + 1;
    if (month <= 9) {
        month = "0" + month
    };
    date = dt.getDate();
    if (date <= 9) {
        date = "0" + date
    };
    ThisDate = year + month + date;

    LastVisitDate = getCookie('VisitDate');
    if (LastVisitDate != null) {
        if (LastVisitDate < LastChangeDate) {} else {}
        setCookie('VisitDate', ThisDate, 365)
    } else {
        window.location = "\agree.htm";
    }
}

$( document ).ready(function() {
   ...