页面更改后不会存储cookie

时间:2017-04-17 20:59:36

标签: javascript jquery cookies

我使用以下代码存储来自表单值的Cookie。当我查看Application>时,我可以看到已经存储了cookie。开发人员工具中的Cookie。当您按此表单上的提交时,它会将您带到调查的开始。调查页面位于表单页面的同一文件路径中。在调查的第一页加载后,我会检查开发人员工具并且不存在cookie。这只是随机发生的,但我发现它在使用Chrome的Macbook上经常发生。我在使用镀铬的电脑上,它从未发生在我身上。

// Script to set cookies
      function setCookie(cname,cvalue,exdays) {
      var d = new Date();
      d.setTime(d.getTime() + (exdays*24*60*60*1000));
          var expires = "expires=" + d.toGMTString();
          document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
      }
    // Script to get cookies
      function getCookie(cname) {
        var name = cname + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.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 "";
      }

    //Check for enabled cookies
    function areCookiesEnabled() {
      document.cookie = "__verify=1";
      var supportsCookies = document.cookie.length >= 1 &&
                            document.cookie.indexOf("__verify=1") !== -1;
      var thePast = new Date(1976, 8, 16);
      document.cookie = "__verify=1;expires=" + thePast.toUTCString();
      return supportsCookies;
    }

    //Set cookies on form submission
    $(document).ready(function() {
        $( "#fn-2" ).change(function() {
          setCookie("firstName", this.value, 30; path=appfire.com/club);
        });
        $( "#ln-2" ).change(function() {
          setCookie("lastName", this.value, 30);
        });
        $( "#eml-2" ).change(function() {
          setCookie("email", this.value, 30);
        });
      });
      $("#wf-form-club_profile_creator").submit(function(e){
        e.preventDefault();
        if (areCookiesEnabled() ) {
          console.log('Cookies are enabled.');
          window.location.replace("http://www.appfire.com/club/quiz/q1");
        } else {
         console.log('Cookies are disabled');
         alert('Cookies appear to be disabled in your browser. To create your A List club profile, please enable cookies and try again. Thanks!');
         window.location.replace("http://www.appfire.com/club");
        }
      });

0 个答案:

没有答案