JavaScript首次访问时同意ToS

JavaScript首次访问时同意ToS,javascript,jquery,html,jquery-ui,dialog,Javascript,Jquery,Html,Jquery Ui,Dialog,我正在尝试制作一个脚本,如果您以前从未访问过该网站,它将重定向到/agree.htm,如果他们单击“确定”,它将不再询问,但如果他们不单击,它将继续重定向到/agree.htm页面。 以下是迄今为止的JavaScript: function doit() { document.getElementById('uuid').innerHTML = generateUUID(); var yes = getUrlVars()["agreed"]; if (yes == "tr

我正在尝试制作一个脚本,如果您以前从未访问过该网站,它将重定向到/agree.htm,如果他们单击“确定”,它将不再询问,但如果他们不单击,它将继续重定向到/agree.htm页面。 以下是迄今为止的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";}
}
函数doit(){
document.getElementById('uuid')。innerHTML=generateUID();
var yes=getUrlVars()[“同意”];
如果(yes==“true”){setCookie('VisitDate',1365);}
如果(是!=“true”){window.location=“/agree.htm”;}
}
函数getUrlVars(){
var vars={};
var parts=window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,函数(m,键,值){
变量[键]=值;
});
返回变量;
}
函数getCookie(函数名){
如果(document.cookie.length>0){
begin=document.cookie.indexOf(name of cokie+“=”);
如果(开始!=-1){
begin+=COKIE的名称。长度+1;
end=document.cookie.indexOf(“;”,begin);
如果(end==-1)end=document.cookie.length;
返回unescape(document.cookie.substring(begin,end));
} 
}
返回null;
}
函数setCookie(cookie名称、值、过期日期){
var ExpireDate=新日期();
ExpireDate.setTime(ExpireDate.getTime()+(expiredays*24*3600*1000));
document.cookie=NameOfCookie+“=”+转义(值)+
((expiredays==null)?“”:“expires=“+ExpireDate.TogmString());
}
函数delCookie(函数名){
if(getCookie(cookie名称)){
document.cookie=NameOfCookie+“=”+
“到期日=周四,1970年1月1日00:00:01格林威治标准时间”;
}
}
函数DoTheCookieStuff(LastChangeDate)
{
dt=新日期();
年份=dt.getYear();如果(年份)
看起来Javascript中列出了所有正确的函数,但是没有调用它们

在页面加载时,您需要检查cookie是否存在。如果不存在,则需要显示模式

我添加了一个按钮来删除cookie,以便您可以进行测试

标记中删除Javascript,并将其替换为以下内容:

Javascript 检验
  • 打开并单击对话框中的确定(这将添加cookie)
  • 重新加载页面。cookie应该存在,您将被重定向到index.html,并显示“删除cookie”按钮(尽可能重复此步骤,它仍应执行此操作)
  • 按下删除cookie按钮并重新加载。现在它应该重定向到agree.htm并再次显示对话框

  • 我忘了提到有:index.html和agree.htm。索引的代码在我的帖子上是第一个,第二个是agree.htm。很抱歉弄错了,你能不能解决这个问题?:)好的,现在应该可以了。我创建了一个多页的演示。如果你有任何问题,请告诉我。
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>StratHaxxs Co. ToS Agreement</title>
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <style>
      .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;
    
    }
    </style>
    
      <script>
      $(function() {
      $('#success').dialog({
        height: 180,
        width: 350,
        modal: true,
        resizable: false,
        dialogClass: 'no-close success-dialog',
        buttons: {
            Ok: function() {
              $( this ).dialog( "close" );
              window.location.href = "/?agreed=true";
            }
          }
    });
      });
      </script>
    </head>
    <body>
    
    <div id="success"  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>
    
    </body>
    </html>
    
    $( document ).ready(function() {
        $('#remove-cookie').click(function() {
            delCookie('agreeCookie');
            $('h1').text('Cookie Removed');
            $('#remove-cookie').hide();
        });
        var cookieStatus = getCookie('agreeCookie');
        console.log(cookieStatus);
        if (cookieStatus != 'true') {
            tryRedirect('agree.htm');
            $('#success').show().dialog({
                height: 180,
                width: 350,
                modal: true,
                resizable: false,
                dialogClass: 'no-close success-dialog',
                buttons: {
                    Ok: function () {
                        setCookie('agreeCookie', true, 100);
                        $(this).dialog("close");
                        window.location.href = "index.html?agreed=true";
                    }
                }
            });
        }
        else {
            tryRedirect('index.html');
            $('#remove-cookie').show();
        }
        function tryRedirect(target) {
            var url = window.location.pathname;
            var filename = url.substring(url.lastIndexOf('/') + 1);
            if (filename != target) {
                window.location.href = target;
            }
        }
    });