1.地址栏传递参数(有两种hash和search)
比如地址栏为 http://www.baidu.com?yzm#yzm
search
var search=location.search.substring(1); //search==yzm#yzm
var search=location.search.substring(1).charAt(0); //search==y
hash
var hash=location.hash; // hash==#yzm
2.cookie传递参数(但https下对cookie要求比较严格)
向cookie存值
document.cookie="yzm=123"
取值
var strCookie=document.cookie;
var arrCookie=strCookie.split("; "); //将多cookie切割为多个名/值对
var userId;
for(var i=0;i<arrCookie.length;i++){ //遍历cookie数组,处理每个cookie对
var arr=arrCookie[i].split("=");
if("yzm"==arr[0]){ //找到名称为userId的cookie,并返回它的值
userId=arr[1];
break;
}
var arrCookie=strCookie.split("; "); //将多cookie切割为多个名/值对
var userId;
for(var i=0;i<arrCookie.length;i++){ //遍历cookie数组,处理每个cookie对
var arr=arrCookie[i].split("=");
if("yzm"==arr[0]){ //找到名称为userId的cookie,并返回它的值
userId=arr[1];
break;
}
}
if (userId == '123') {
document.cookie="yzm=null"; //将cookie清空
}
if (userId == '123') {
document.cookie="yzm=null"; //将cookie清空
}
3.session storage传递参数
//存值
window.sessionStorage.setItem('aaa', 'bbb');
//取值
var arr = sessionStorage.getItem('aaa');
window.sessionStorage.setItem('aaa', 'bbb');
//取值
var arr = sessionStorage.getItem('aaa');