您的位置:首页 > Web前端 > JavaScript

如何使用js来实现通过href完成多个html页面之间的传递参数

2016-09-20 14:41 1041 查看
有些时候我们需要在一连串的网站跳转中都要使用某些参数值,那么楼主分享的是:在不使用php和数据库的情况下,如何通过简单的js代码来实现多个网站之间的数值传递。(适合html+js菜鸟参考,高手勿喷~o(^▽^)o)

本文所要实现的功能如下:

1、A.html将某些值或者参数(name和time的值,分别是li和morning)通过href传递给b.html。

2、B.html成功接收到这些值,然后继续将这些值通过href传递下去给c.html。

3、C.html成功接收到这些值,并显示出来。

A.html

<body>
<a href="B.html?name=li&time=morning">传递按钮</a>  //传递name=li和time=morning
</body>


B.html

<body>
<a href="C.html" id="bbb">传递按钮</a>

<script>
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}

var Request = new Object();
Request = GetRequest();
var a, b;
a = Request['name'];
b = Request['time'];
document.getElementById("bbb").href = 'C.html?name='+a+'&time='+b;//修改href,继续传递
</script>
</body>


C.html

<body>
<p id="ccc"></p>

<script>
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}

var Request = new Object();
Request = GetRequest();
var a, b;
a = Request['name'];
b = Request['time'];
document.getElementById("ccc").write(name) ;
document.getElementById("ccc").write(time) ;
</script>
</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html javascript