ReadCookie_sns = function(){ ReadCookie("myname"); }
出错,提示:Unexpected token u in JSON at position 0
改为:
ReadCookie_sns = function(){ return ReadCookie("myname"); }
好了。 加了return, 就好了。请问为什么?
-----------------下面是ReadCookie方法。
ReadCookie = function(cookieName) {
var theCookie = "" + document.cookie;
var ind = theCookie.indexOf(cookieName);
if (ind == -1 || cookieName == "") return "";
var ind1 = theCookie.indexOf(';', ind);
if (ind1 == -1) ind1 = theCookie.length;
/*读取Cookie值*/
return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
return 语句终止函数的执行,并返回一个指定的值给函数调用者。
如果忽略,则返回 undefined。
https://developer.mozilla.org...
function(){ ReadCookie("myname"); }返回的是undefined。function(){ return ReadCookie("myname"); }才是返回unescape(theCookie.substring(ind + cookieName.length + 1, ind1));当前操作是要赋值给变量,没有return会报错,如果只是单纯执行整个语句的后面部分,是不会报错的