HTML页面之间跳转与传值(JS代码)
2019-12-04 本文已影响0人
回忆不死我们不散
跳转的方法如下:
方法一:
window.location.href = "b.html";
方法二(返回上一个页面,这个应该不算,先放在这):
window.history.back(-1);
方法三:
self.location = "b.html";
方法四:
top.location = "b.html";
有关问题的思考:
第一,为什么给window.location和window.location.href赋值时一样的,都可以跳转?
思考:
location是location.href的简写,无论是访问值还是赋值。
从功能上,location等于location.href;
但从本体论上,location是一个对象,location.href是它的一个属性。
这种怪异的行为应该是为了兼容无疑。
第二,给location赋值的时候,如果跳转的页面不是在同一个目录下,需要把完整的URL写上。如:当前location.href为https://www.google.com/ ,如果要跳转到https://www.baidu.com/, 就不能只是www.baidu.com,必须把URL写完整。
传值的方法如下:
方法一:URL传参(?后面的参数)(去哪儿网笔试题,把URL后面的参数解析为对象)
window.location.href = "https://www.google.com/search?q=hello&oq=hello"
function parseURL(url){
var url = url.split("?")[1];
var para = url.split("&");
var len = para.length;
var res = {};
var arr = [];
for(var i=0;i<len;i++){
arr = para[i].split("=");
res[arr[0]] = arr[1];
}
return res;
}
方法二:cookie传参
function setCookie(cname,cvalue,exdays){
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname){
var name = cname + "=";
var ca = document.cookie.split(";");
for(var i=0;i<ca.length;i++){
var c = ca[i];
while(c.charAt(0)==' '){
c = c.substring(1);
}
if(c.indexof(cname) == 0){
return c.substring(name.length,c.length);
}
}
return "";
}
方法三:H5中Web Storage中的localStorage对象
localStorage.setItem("lastname","lu");
或者
localStorage.lastname = "lu";
localStorage.getItem("lastname");
或者
localStorage.lastname;
装载自:https://www.cnblogs.com/mingmingcome/p/5926254.html
第一种形式 [[ ]]
[["12","32","56","10","67","89","12","12","8","42","33","9"]]
怎么处理上面的数据,处理以后是以下的形式;因为我要在页面上循环遍历
["12","32","56","10","67","89","12","12","8","42","33","9"]
解决方案 eval(数据)
eval(数据)可以对数据进行运算,例如返回数组对象,自动去掉外面多于的[]
this.tableData=eval(e.data);
第二种形式 {"属性名":"属性值"}
{"sz":["12","32","56","10","67","89","12","12","8","42","33","9"]}
我要获取["12","32","56","10","67","89","12","12","8","42","33","9"]
解决方案
var obj = eval('(' + e.data + ')');
alert(obj.sz);
this.tableData=obj.sz;