Search code examples
javascriptjquerycookiessetcookie

How to replace document.cookie to Cookies.set (with latest jquery cookie plugin)


I am trying to replace document.cookie to Cookies.set (with latest jquery cookie plugin) but not working. Plugin link here

    <script>

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

    function getCookie(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==name)
            {
            return unescape(y);
            }
          }
    }
    </script>

Solution

  • Here is the equivalent:

    <script>
    
        function setCookie(name,value,exdays) {
            Cookies.set( name, value, {
                expires: exdays,
                // path: "" or
                // path: "/"
                // In version 2 the path is default to "/"
                // In version <= 1 the path is default to "" (path of the current page)
            })
        }
    
        function getCookie(name) {
            return Cookies.get( name );
        }
    </script>