Chrome 和 IE 中的 JSON.parse 语法错误
我正在尝试使用 jquery cookie 插件来有效地“记住”由 jscrollpane 插件创建的滚动窗格的位置。
基本上我希望 jscrollpane 查看 cookie 并根据保存的值设置初始水平位置。然后在位置改变时更新 cookie。
我一开始使用数组,但现在使用对象。关键是 div #id,我对其进行编码以反映其显示的帖子类别...这样它将是唯一的。键的值是 jscrollpane 踢出的水平位置。
我认为最好将我想要存储为 cookie 的对象转换为 JSON 字符串,但是当我尝试使用 JSON.parse(cookie) 将其转换回对象时,我在 IE 和 Chrome 中遇到语法错误。
jQuery(function($){
//load the cookie
var cookie = $.cookie('xpos');
//Load the saved values or a new array if null.
var xpositions = cookie ? JSON.parse(cookie) : new Object();
console.log(xpositions);
// Loop over each scroll-pane
$( ".scroll-pane" ).each( function( index ){
$(this).jScrollPane({showArrows: true, autoReinitialise: true}); //initialize jscrollpane
var api = $(this).data('jsp'); //access jscrollpane api
//var catID = parseInt($(this).attr('id').match(/[0-9]+/)); //grab cat_ID which we've stored as part of the div id#
var catID = $(this).attr('id');
if( typeof xpositions[catID] != "undefined" ) {
console.log(catID +" = element exists in array and position = " + xpositions[catID] );
api.scrollToX(xpositions[catID]); //set scroll-pane position to position saved in cookie
}
$(this).bind('jsp-scroll-x',function(event, scrollPositionX){ //change cookie on scroll event
xpositions[catID] = scrollPositionX;
console.log(catID + " = " + scrollPositionX);
//set the cookie with array of x-positions, expires after 7 days
$.cookie('xpos', JSON.stringify(xpositions), { expires: 7, path: '/' });
}
);
}); //end each
});
你可以在这里查看实时版本: http://www.testtrack.tv/
编辑:我也应该提到这似乎可以在我的本地 XAMPP 服务器上运行,但仍然无法正常运行。谢谢!
编辑:为什么在 SO 上发帖似乎给我指明了更好的方向?从那时起,我发现 jookie 插件可以在 cookie 插件因我的对象而失败的情况下正常工作。
http://joncom.be/code/jquery-jookie/
我的新代码是这样的:
// initialise a cookie that lives for 1 week
$.Jookie.Initialise("xposition", 60*24*7);
// Loop over each scroll-pane
$( ".scroll-pane" ).each( function( index ){
$(this).jScrollPane({showArrows: true, autoReinitialise: true}); //initialize jscrollpane
var api = $(this).data('jsp'); //access jscrollpane api
var catID = $(this).attr('id');
var xpos = $.Jookie.Get("xposition", catID);
if(xpos) {
api.scrollToX(xpos); //set scroll-pane position to position saved in cookie
}
$(this).bind('jsp-scroll-x',function(event, scrollPositionX){ //change cookie on scroll event
// set a value to the cookie
$.Jookie.Set("xposition", catID, scrollPositionX);
}
);
}); //end each
i'm trying to use the jquery cookie plugin to effectively 'remember' the position of a scrollpane created by the jscrollpane plugin.
basically i want jscrollpane to look at the cookie and set the initial horiztonal position based on the saved value. and then on change of position, update the cookie.
i started out w/ arrays, but am now using objects. the key is the div #id, which i coded to reflect the category of posts it is displaying... this way it will be unique. the key's value is the horizontal position that jscrollpane kicks out.
i thought it would work best to turn the object i want to store as a cookie into a JSON string, but when i try to convert it back into an object using JSON.parse(cookie) i get syntax errors in IE and Chrome.
jQuery(function($){
//load the cookie
var cookie = $.cookie('xpos');
//Load the saved values or a new array if null.
var xpositions = cookie ? JSON.parse(cookie) : new Object();
console.log(xpositions);
// Loop over each scroll-pane
$( ".scroll-pane" ).each( function( index ){
$(this).jScrollPane({showArrows: true, autoReinitialise: true}); //initialize jscrollpane
var api = $(this).data('jsp'); //access jscrollpane api
//var catID = parseInt($(this).attr('id').match(/[0-9]+/)); //grab cat_ID which we've stored as part of the div id#
var catID = $(this).attr('id');
if( typeof xpositions[catID] != "undefined" ) {
console.log(catID +" = element exists in array and position = " + xpositions[catID] );
api.scrollToX(xpositions[catID]); //set scroll-pane position to position saved in cookie
}
$(this).bind('jsp-scroll-x',function(event, scrollPositionX){ //change cookie on scroll event
xpositions[catID] = scrollPositionX;
console.log(catID + " = " + scrollPositionX);
//set the cookie with array of x-positions, expires after 7 days
$.cookie('xpos', JSON.stringify(xpositions), { expires: 7, path: '/' });
}
);
}); //end each
});
you can check out the live version here: http://www.testtrack.tv/
edit: i should also mention that this seems to work on my local XAMPP server, but still fails live. thanks!
edit: why is it that posting on SO seems to point me in a better direction? i have since found the jookie plugin to just flat out WORK where the cookie plugin was failing w/ my object.
http://joncom.be/code/jquery-jookie/
my new code is this:
// initialise a cookie that lives for 1 week
$.Jookie.Initialise("xposition", 60*24*7);
// Loop over each scroll-pane
$( ".scroll-pane" ).each( function( index ){
$(this).jScrollPane({showArrows: true, autoReinitialise: true}); //initialize jscrollpane
var api = $(this).data('jsp'); //access jscrollpane api
var catID = $(this).attr('id');
var xpos = $.Jookie.Get("xposition", catID);
if(xpos) {
api.scrollToX(xpos); //set scroll-pane position to position saved in cookie
}
$(this).bind('jsp-scroll-x',function(event, scrollPositionX){ //change cookie on scroll event
// set a value to the cookie
$.Jookie.Set("xposition", catID, scrollPositionX);
}
);
}); //end each
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xpos=%7B%22cat-45%22%3A0%2C%22cat-48%22%3Anull%7D
是我访问您的页面时您设置为我的 cookie 的内容; JSON 解析器无法解析它。你希望它是 xpos={..object stuff here..}。本质上,如果您无法手动将字符串复制到变量中,解析器就会遇到麻烦。
xpos=%7B%22cat-45%22%3A0%2C%22cat-48%22%3Anull%7D
Is what you set as my cookie when I visited your page; a JSON parser isn't going to be able to parse that. You want it to be xpos={..object stuff here..}.Essentially, if you can't copy the string into a variable manually, the parser will have trouble.
您尝试过进行评估吗?
Have you try to do an eval ?