首页 > 代码库 > 两个页面之间的数据传递方法
两个页面之间的数据传递方法
1:通过URL传参(a页面<a href="http://www.mamicode.com/b.html?n=zhangsan&p=123456">点击跳转到b页面</a>)
b页面中的js代码:
function parseURL(url){
var url = url.split("?")[1];//分割字符串并取
console.log(url);
var para = url.split(/&|=/);
console.log(para);//["n", "zhangsan", "p", "123456"]
var len = para.length;
var arr=[];
for(var i=0;i<len;i++){
if(i%2!=0){
arr.push(para[i]);//取偶数项的数组项并将它添加到定义的数组中去
}
}
return arr;
}
var url=window.location.href;
parseURL(url)
2_方法二:cookie传参______________________________________________________________....
function setCookie(name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
if (expires instanceof Date){
cookieText += ";expires="+expires;
}
if (path) {
cookieText += ";path="+path;
}
if (domain) {
cookieText += ";domain="+domain;
}
if (secure) {
cookieText += ";" + secure;
}
document.cookie = cookieText;
return decodeURIComponent(cookieText);
}
var date = newDate();
date.setDate(date.getDate() +7);
var expires = date.toUTCString();
var xx=setCookie("nameccff","zhangsan",date ,"/","baidu.com","http://")
方法三:H5中Web Storage中的localStorage对象
localStorage.setItem("lastname","lu");
或者
localStorage.lastname = "lu";
localStorage.getItem("lastname");
或者
localStorage.lastname;
两个页面之间的数据传递方法