gpt4 book ai didi

javascript - 存储和调用 cookie Javascript 的问题

转载 作者:可可西里 更新时间:2023-11-01 02:39:09 25 4
gpt4 key购买 nike

因此,我试图保存并加载一个包含产品详细信息列表的 cookie,然后将这些产品详细信息显示到页面上。

然而,当我运行这个时,我收到的是一个

ReferenceError: Can't find variable: GetProductPrice

尝试读取我使用 GetProductPrice() 函数存储的 cookie 时。

Cookie 存储程序在网站首次加载时运行。它创建了一个产品列表,我稍后打算将其用作购物车的一部分。然后它应该保存这个 cookie,以便在用户转到不同页面时可以再次访问它。这样,如果用户将商品添加到购物车,购物车变量将不会在以后重置。但是正如我之前所说,这是行不通的。加载和保存 cookie 函数是由其他人在 Stack Overflow 上创建的,并且工作正常。

这是 cookie 保护程序的代码,它应该只在用户第一次访问网站时运行:

function setUpProducts() { //in here we define each of the products when we first start up. This way we know what products the customer has access to.
setUpPages();
try {
hasRun = getCookie('hasRunCookie'); //get the has run cookie from before, or at least try to
} catch {
hasRun = 0;
}
//if (hasRun != 1){ //only run this the first time, otherwise we dont need to.
if (hasRun != 1) {
hasRun = 1;
createCookie('hasRunCookie', hasRun);
var dataList = []; //array for holding the raw data
var nameList = [];
var priceList = [];
var amountList = []; //lists for temporrily holding data before it is put in the product list's product objects.
var set1 = 0; //allows differentiating between different values in the raw data
var productIds = 0; //product ID to differentiate between products easily

$.get('../productdata.txt', function(data) { //the text file product data contains all the product infomation so that it can be changed at a moments notice.
//var fileDom = $(data);

dataList = data.split("\n");
$.each(dataList, function(n, elem) {
$('#myid').append('<div>' + elem + '</div>');
});
});

var i;
for (i = 0; i < dataList.length; i++) { //this gets the infomation from the text file and loads it into the products
if (set1 == 0) {
nameList.push(dataList[i]);
set1 = 1;
} else if (set1 == 1) {
priceList.push(dataList[i]);
set1 = 0;
}
}
while (productIds != 8) { //make the products, programing counting always starts at 0, so 8 is actually the 9th number.
var productObject = {
productID: productIds,
productName: nameList[productIds],
productPrice: priceList[productIds],
purchasedAmount: 0
};
productList.push(productObject); //put each product into the product list.
productIds += 1;
}
var json_str = JSON.stringify(productList); //bottle and store our list of products
createCookie('ProductListCookie', json_str);
//}

}

这是用于加载 cookie 并在相关产品页面上显示产品价格的代码:

function GetProductPrice(productIdz) {
var json_str = getCookie('hasRunCookie');
productList = JSON.parse(json_str);
for (i = 0; i < productList.length; i++) {
if (productList[i].productID == productIdz) {
productHolder = productList[i];
document.getElementById("price").innerHTML = productHolder.productPrice;
}
}
}

最佳答案

使用localStorage

//Set
localStorage.setItem('ProductListCookie', json_str);

//Get
var json_str = localStorage.getItem('ProductListCookie');

关于javascript - 存储和调用 cookie Javascript 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50557862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com