设置切换菜单的cookie

时间:2018-03-20 12:30:07

标签: javascript php jquery cookies

我尝试根据每次点击按钮设置C​​ookie。我有一个带切换规则的按钮。当用户选择打开菜单切换时,应该保存cookie大约一周...下次(第二次单击ot按钮 - 切换菜单隐藏)它必须更改cookie值... 当cookie值为1时 - 菜单必须始终打开,当值为2时 - 菜单必须始终关闭..

按钮:

 <ul class="nav navbar-nav navbar-right">
     <li>
        <div class="navbar-btn btn-group">
          <a href="#" onclick="setMenuSm('1')" id='toggle-button' class="topbar-menu-toggle btn btn-sm" data-toggle="button">
          <span class="ad ad-wand"></span>
         </a>
        </div>
    </li>
</ul>

和java脚本:

    function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function setMenuSm(){
      var cssSelected = $("#toggle-button").hasClass("active");
      if (cssSelected !== true) {
          setCookie("selectedCSS", "menuopen", 3);
      }else{
          setCookie("selectedCSS", "menuclose", 3);
      }
}

$(document).ready(function() {
    getCookie("selectedCSS");
    console.log(getCookie("selectedCSS"));
})

...现在当点击加载页面控制台给我&#34; menuopen&#34;当点击按钮打开灰色窗口,如加载和无事可做......

0 个答案:

没有答案