1. 程式人生 > >如何使用js來實現通過href完成多個html頁面之間的傳遞引數

如何使用js來實現通過href完成多個html頁面之間的傳遞引數

有些時候我們需要在一連串的網站跳轉中都要使用某些引數值,那麼樓主分享的是:在不使用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>