1. 程式人生 > >HTML 頁面傳值及另一個頁面接收值

HTML 頁面傳值及另一個頁面接收值

a.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>a頁面</title>
<script type="text/javascript" src="link/jquery.min.js"></script>
<script type="text/javascript">
function to(){  
    var getval ="valueTest中文字元";
    var getval2 ="valueTest2";
    window.location.href="b.html?valus="+getval;
    // window.location.href="b.html?valus="+getval+"&value2="+getval2;
} 
</script>

</head>
<body>
<button onclick="to()">傳值</button>
</body>
</html>

b.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>b頁面</title>
<script type="text/javascript" src="link/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
console.log(decodeURI(location.search.concat()));
manyValues();
$("#show").html(oneValues());
})

//接收一個值
function oneValues(){
    var result;
    var url=decodeURI(window.location.search); //獲取url中"?"符後的字串  
    if(url.indexOf("?") != -1) {
        result = url.substr(url.indexOf("=")+1);
    }
    return result;
}

//接收多值
function manyValues() {
    var url=decodeURI(window.location.search);
    if(url.indexOf("?")!=-1) {
        var str = url.substr(1);
        strs = str.split("&"); 
        var key = new Array(strs.length);
        var value = new Array(strs.length);
        for(i = 0;i < strs.length;i++){
            key[i] = strs[i].split("=")[0]
            value[i] = unescape(strs[i].split("=")[1]); 
            alert(key[i]+"="+value[i]);
        } 
    } 
}

</script>
</head>
<body>
<span>接收的值是:</span>
<span id="show"></span>
</body>
</html>

注:

中文傳輸:可以在頁面a用 encodeURI 編碼url  在b頁面用 decodeURI 解碼url

https://blog.csdn.net/u012211821/article/details/78919126