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(url.indexOf("?") + 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

上一篇:力扣-第14题--最长公共前缀


下一篇:【力扣】难度【简单】14. 最长公共前缀