最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery cookie value is %5Bobject%20Object%5D - Stack Overflow

programmeradmin5浏览0评论

I have a simple html page and a js script: HTML page:

<body>
<input type="text" id = "content">
<button type="button" id="btn"> Save </button>
</body>

Javascript:

$(document).ready( function(){
    var cook = $.cookie('theName',  { path: '/'});
     if ( cook )
        alert(cook); 
    $('#btn').click(function(){
        var theName = $('#content').val();
        alert(v.val());
        $.cookie('theName', theName, { path: '/', expires: 7 });
        alert("Cookie done");
    });
});

Libraries:

<script type="text/javascript" src=".9.1.min.js"> </script>
<script type="text/javascript" src = ".cookie.js"> </script>

It should save my name and when I hit refresh to show my name. The only problem is when I try to read the cookie, instead of name shows %5Bobject%20Object%5D.

Thank you.

I have a simple html page and a js script: HTML page:

<body>
<input type="text" id = "content">
<button type="button" id="btn"> Save </button>
</body>

Javascript:

$(document).ready( function(){
    var cook = $.cookie('theName',  { path: '/'});
     if ( cook )
        alert(cook); 
    $('#btn').click(function(){
        var theName = $('#content').val();
        alert(v.val());
        $.cookie('theName', theName, { path: '/', expires: 7 });
        alert("Cookie done");
    });
});

Libraries:

<script type="text/javascript" src="http://code.jquery./jquery-1.9.1.min.js"> </script>
<script type="text/javascript" src = "https://raw.github./carhartl/jquery-cookie/master/jquery.cookie.js"> </script>

It should save my name and when I hit refresh to show my name. The only problem is when I try to read the cookie, instead of name shows %5Bobject%20Object%5D.

Thank you.

Share Improve this question edited May 3, 2013 at 10:11 Alin Ciocan asked May 3, 2013 at 9:31 Alin CiocanAlin Ciocan 3,0884 gold badges36 silver badges48 bronze badges 4
  • Have you tried: var cook = $.cookie('theName'); ? – A. Wolff Commented May 3, 2013 at 9:35
  • Yes, but then 'cook' is undefined. – Alin Ciocan Commented May 3, 2013 at 9:50
  • %5Bobject%20Object%5D is [object Object], i.e. the default string representation of an object. – Felix Kling Commented May 3, 2013 at 9:55
  • I agree. But how can I get the value of the cookie. – Alin Ciocan Commented May 3, 2013 at 9:58
Add a ment  | 

3 Answers 3

Reset to default 4

do:

$(document).ready( function(){
    var cook = $.cookie('theName'); //get from cookie if exists
     if ( cook )
        alert(cook); 
    $('#but').click(function(){
        var theName = $('#content').val();
        alert(theName);
        $.cookie('theName', theName, { expires: 7, path: '/' }); //set the cookie
        alert("Cookie done");
    });
});

Updated:

try adding:

$.cookie.raw = true;
var cook = $.cookie('theName',  { path: '/'});

This overwrites the cookie with the string representation of { path: '/'}.

To actually retrieve the existing cookie just pass the name:

var cook = $.cookie('theName');

There is no point in passing the path anyway - if you are outside the path for which the cookie is set you simply don't get it at all.

Try This.

<!DOCTYPE html>
<html>
<head>
<script>
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}

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 checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
  {
  alert("Wele again " + username);
  }
else
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>

Hope this helps.

Source:Here

发布评论

评论列表(0)

  1. 暂无评论