Cookie没有在页面加载时设置

时间:2015-08-07 13:18:58

标签: javascript jquery asp.net-mvc cookies

首次访问用户时,我需要设置一些Cookie。所以我清除浏览器的cookie,然后尝试加载我的项目。清除cookie后。 Cookie未在页面加载时设置。我正在使用Razor

开发MVC 5

这是我的javascript代码

  <script src="~/Content/JavaScript/jquery.js"></script>
        <script src="~/Content/JavaScript/jqueryui/jquery-ui-1.10.4.custom.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function () {
            getCookie("user_id");
            if (getCookie("user_id") == "") {
                var user_id = guid();
                setCookie("user_id", user_id, 365);
            }

            var currency_id = getCookie("currency_id")
            if (currency_id == "" || currency_id == null) {
                setCookie("currency_id", "1", 365);
                setCookie("currency_code", "INR", 365);
                setCookie("currency_rate", "1", 365);
            }
            else {
                ChangeCurrencyValue(currency_id)
            }
            ShowCart();
            ShowWishList();

        });
        </script>
  <script type="text/javascript">
     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;
        }
        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 guid() {
            function s4() {
                return Math.floor((1 + Math.random()) * 0x10000)
                  .toString(16)
                  .substring(1);
            }
            return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
              s4() + '-' + s4() + s4() + s4();
        }
        </script>

我已经尝试将代码置于<head>阻止其无法正常工作

1 个答案:

答案 0 :(得分:0)

我猜的问题是脚本执行顺序。您正在尝试调用位于不同脚本块中的函数,并在将其注册到浏览器之前调用它。

尝试将所有函数和其他代码放在一个脚本块中而不是不同的块中:

<script src="~/Content/JavaScript/jquery.js"></script>
<script src="~/Content/JavaScript/jqueryui/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function () {
        getCookie("user_id");
        if (getCookie("user_id") == "") {
            var user_id = guid();
            setCookie("user_id", user_id, 365);
        }

        var currency_id = getCookie("currency_id")
        if (currency_id == "" || currency_id == null) {
            setCookie("currency_id", "1", 365);
            setCookie("currency_code", "INR", 365);
            setCookie("currency_rate", "1", 365);
        }
        else {
            ChangeCurrencyValue(currency_id)
        }
        ShowCart();
        ShowWishList();

    });
  // -------##########---remove the closing script tag---#######
 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;
    }
    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 guid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
              .toString(16)
              .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
          s4() + '-' + s4() + s4() + s4();
    }
    </script>