我想知道如何使用JavaScript将变量从一个页面传递到另一个页面
下面是我的代码:
var currentBasketPrice = 0;
var quotes = new Array(
"A good painting to me has always been like a friend. It keeps me company, comforts and inspires.",
"A man paints with his brains and not with his hands.",
"A picture is a poem without words.",
"A work of art is the unique result of a unique temperament.",
"An artist cannot fail; it is a success to be one.");
var quotePerson = new Array(
"Hedy Lamarr",
"Michaelangelo",
"Horace",
"Oscar Wilde",
"Charles Horton Cooley");
var imgSrc = new Array ("images/hangings/1.jpg","images/hangings/2.jpg");
var prices = new Array (100,50);
var sizes = new Array ("450*350","100*50");
var inBasketPrices = [];
var inBasketNames = [];
function load(displayQuotes, displayBasket, displayProductInfo)
{
if (displayQuotes==true)//quotes
{
var quotenum = Math.floor(Math.random()*5 );
document.getElementById('quote').innerHTML= quotes[quotenum];
document.getElementById('quoteperson').innerHTML=" - " + quotePerson[quotenum];
}
if(displayBasket==true)
{
var list = $('#basketsidebar ul');
list.append('<button type="button">Checkout</button>');
}
if(displayProductInfo==true)
{
for (var i = 0; i < prices.length; i++)
{
var list = $('#products ul');
list.append('<li><img src="'+imgSrc[i]+'" width="525px" height="350px"/></li>');
list.append('<li>Price: £' + prices[i] +'</li>');
list.append('<li>Size: ' + sizes[i] +'</li>');
list.append('<button type="button" onclick="addToBasket(' + i + ')">Add To Basket</button>');
}
}
}
function addToBasket(itemAdded)
{
inBasketPrices.push(prices[itemAdded]);
var list = $('#basketsidebar ul');
list.append('<li>Price: £' + inBasketPrices[itemAdded] +'</li>');
}我想让它这样做,当结帐按钮被点击时,用户被定向到一个新的页面,并且当前在篮子中的项目被继续进行。目前,只有商品的价格被添加到购物篮中。
我不知道如何做到这一点,所以如果有人可以帮助它,将不胜感激。
发布于 2012-01-28 03:22:44
Javascript是一种由浏览器在定义的页面上执行的语言。页面之间的关系由HTTP协议处理,因此从一个页面传播数据到另一个页面应该使用该协议来完成。
HTTP允许您以三种方式共享数据:
GET格式的数据很容易用Javascript编写,您只需将?var1=value&var2=value2附加到您的网址。您可以通过使用服务器端编程语言或通过分析URL来检索数据(请参阅here)。对于你的应用程序来说,POST可能会有点棘手,但仍然可以完成。Cookie也可以是一种选择,但有点不同。Cookie保存在客户端计算机上,而不是将HTTP请求作为GET/POST进行处理。您可以通过Javascript操作cookies,如this tutorial中所示。
发布于 2012-01-28 03:22:12
欢迎使用StackOverflow!
要将信息从一个页面移动到另一个页面,有一些技术:
中查找购物车。
服务器端软件上的书籍讨论购物车是很常见的,因为购物车对人们来说是一个常见的问题。您使用的是什么服务器端软件?
还在布尔表达式(如if语句)中,不需要将变量与"true“进行比较。换句话说:
if (displayQuotes==true)
// is the exact same as
if (displayQuotes)发布于 2012-01-28 03:21:06
我不确定你能不能用javascript做到这一点。尝试使用PHP,特别是$_SESSION。
https://stackoverflow.com/questions/9038728
复制相似问题