所以,伙计们,我试着让这个工作。当我单击页面上的按钮时,它必须将数组传输到另一个页面才能显示它。我的函数 addProduct、showProduct、addItem 和 button 工作。但是我来自数组的数据不会传递到其他页面。因此,当我单击页面 A 中的按钮时,会向该数组添加一个值。但在 B 页上,它显示数组为空
var products = new Array();
window.addEventListener("load", loadPage, false);
window.addEventListener("load", addButton, false);
window.addEventListener("load", showProducts, false);
function addButton() {
var button = document.getElementById("sell");
button.addEventListener("click", addItem, false);
}
function loadPage(){
getCookie();
}
function getCookie(){
var elements = document.cookie.split('=');
var products_cookie = elements[1].split('%');
for(var i=0;i < products_cookie.length-1;i++) {
var tmp = products_cookie[i].split('$');
addProduct(tmp[0], tmp[1], tmp[2]);
}
}
function setCookie(){
var date = new Date();
date.setTime(date.getTime()+(2592000)); //expire time = 30 dagen nadat cookie aangemaakt is
var expires = "; expires="+date.toGMTString()
var text = "";
for (var i = 0; i < products.length; i++){
text+= products[i][0] + "$" + products[i][1] + "$" + products[i][2] +"%"; // slaat naam $ aantal $ prijs op
}
document.cookie = "tradepoint"+text+expires;
}
function addProduct(pName, pAmount, pPrice){
var product = new Array(pName, parseInt(pAmount), pPrice);
products[products.length] = product;
setCookie();
}
function addItem(){
addProduct("andrej", 2, 1);
alert(products + " " + products.length);
}
function showProducts(){
if (products.length != 0){
document.getElementById("shopList").innerHTML = "<ul><li>Full</li></ul>";
}
else{
document.getElementById("shopList").innerHTML = "<ul><li>Empty</li></ul>";
}
}