如果您已经使用过任何长度的JavaScript,那么您就知道Internet Explorer没有为Array.prototype.indexOf()[包括Internet Explorer 8]实现ECMAScript函数.这不是一个大问题,因为您可以使用以下代码扩展页面上的功能.
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
我什么时候应该实现这个?
我应该使用以下检查将其包装在我的所有页面上,检查是否存在原型函数,如果不存在,请继续并扩展Array原型?
if (!Array.prototype.indexOf) {
// Implement function here
}
Run Code Online (Sandbox Code Playgroud)
或者浏览器检查,如果它是Internet Explorer,那么只需实现它?
//Pseudo-code
if (browser == IE Style Browser) {
// Implement function here
}
Run Code Online (Sandbox Code Playgroud) javascript internet-explorer cross-browser internet-explorer-8
如何在cookie中保存JSON数据?
我的JSON数据看起来像这样
$("#ArticlesHolder").data('15', {name:'testname', nr:'4',price:'400'});
$("#ArticlesHolder").data('25', {name:'name2', nr:'1', price:'100'});
$("#ArticlesHolder").data('37', {name:'name3', nr:'14', price:'60'});
Run Code Online (Sandbox Code Playgroud)
我想做点什么
var dataStore = $.cookie("basket-data", $("#ArticlesHolder").data());
Run Code Online (Sandbox Code Playgroud)
和检索数据我想将其加载到$("#ArticlesHolder")像
$.each($.cookie("basket-data"), function(i,e){
$("#ArticlesHolder").data(i, e);
});
Run Code Online (Sandbox Code Playgroud)
有没有人知道我是在正确的轨道上还是应该以其他方式完成?简单地说,我如何从cookie中提取和提取json数据?
我正在打开一个基于此如何在jquery cookie中存储数组的新线程?.我正在使用almog.ori的功能:
var cookieList = function(cookieName) {
//When the cookie is saved the items will be a comma seperated string
//So we will split the cookie by comma to get the original array
var cookie = $.cookie(cookieName);
//Load the items or a new array if null.
var items = cookie ? cookie.split(/,/) : new Array();
//Return a object that we can use to access the array.
//while hiding direct access to the declared items array
//this …Run Code Online (Sandbox Code Playgroud)