2011-06-26 75 views

回答

1

你要麼需要通過GET或POST發送變量到下一個頁面

將它們保存在cookie中,一數據庫或服務器上的文件。


假設變量是show=1page=phones


您可以發送變量是這樣的:

var show = 1; 
var page = 'phones'; 
$('#link').click(function(){ 
    top.location.href = 'page.html?show='+show+'&=page'+phones+''; 
}); 

然後,你可以用任何像PHP或ASP一個服務器端語言獲取下一個頁面上的變量,也可以創建一個智能功能JavaScript來獲取URL的您需要的部分:

function getParam(name) { 
    return decodeURI(
     (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] 
    ); 
} 

,並使用它像這樣(在page.html中):

var show = getParam('show'); 
var page = getParam('page'); 

如果您想變量保存在用戶瀏覽器中的cookie,你可以使用這些功能:

function getCookie(c_name) 
{ 
var i,x,y,ARRcookies=document.cookie.split(";"); 
for (i=0;i<ARRcookies.length;i++) 
    { 
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); 
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); 
    x=x.replace(/^\s+|\s+$/g,""); 
    if (x==c_name) 
    { 
    return unescape(y); 
    } 
    } 
} 

function setCookie(c_name,value,exdays) 
{ 
var exdate=new Date(); 
exdate.setDate(exdate.getDate() + exdays); 
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
document.cookie=c_name + "=" + c_value; 
} 

,並利用它們是這樣的:

在第一頁:

var show = 1; 
var page = 'phones'; 
setCookie(''+show+'',show,5); // expire after 5 days 
setCookie(''+page+'',page,5); // expire after 5 days 
// now redirect to other page like in the first example 
$('#link').click(function(){ 
    top.location.href = 'page.html'; 
}); 

在你的第二頁:

var show = getCookie('show'); 
var page = getCookie('page'); 

if (show === 1) { 
    // Do whatever you like, because cookie 'show' is 1 
} 

我希望它有幫助!