原博: https://blog.csdn.net/qq_28074159/article/details/78813662
page1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>page1</title>
</head>
<body>
<input type="text" value="要传的值是a" id="testInput" >
<input type="button" value="点击" "toPage2()" >
<script type="text/javascript">
function toPage2(){
var input_val = document.getElementById("testInput").value;
// 传的数据中包含中文,用 encodeURI() 将链接编码
window.location.href = encodeURI("page2.html?value=" + input_val);
}
</script>
</body>
</html>
page2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>page2</title>
</head>
<body>
<script type="text/javascript">
var thisURL = decodeURI(document.URL); // 获取当前页面的 url, 用decodeURI() 解码
var getVal = thisURL.split('?')[1].split("=")[1];
console.log(getVal); // 要传的值是a
</script>
</body>
</html>
注意:
如果传的数据中有中文,会出现乱码,解决办法如下:
在 page1 页面中用 encodeURI 将 url 编码
在 page2 页面中用 decodeURI 将 url 解码即可。
作者:fuxi0o_
来源:CSDN
原文:https://blog.csdn.net/qq_28074159/article/details/78813662
版权声明:本文为博主原创文章,转载请附上博文链接!