js实现页面跳转,页面A跳到另一个页面B,以及页面传值

第一个页面: (tes1.html)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>页面1</title>
</head>
<SCRIPT LANGUAGE="JavaScript">
    function test(){
        var s = document.getElementById("txt");
        location.href="test2.html?"+"txt="+encodeURI(s.value);
    }
</SCRIPT>
<body>

<input type="text" id="txt">
</br>
<input type="button" value="TEST" onclick="test()"/>

</body>
</html>

第二个页面:(test2.html)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>页面2</title>
</head>

<body>
  <script language="javascript" type="text/javascript">
  var loc = location.href;
  var n1 = loc.length;//地址的总长度
  var n2 = loc.indexOf("=");//取得=号的位置
  var id = decodeURI(loc.substr(n2+1, n1-n2));//从=号后面的内容
  alert(id);
  //document.write(id)
  </script>
</body>
</html>
注:中文传输:可以在页面a用encodeURI 编码url  在b页面用decodeURI解码url

猜你喜欢

转载自blog.csdn.net/qq_37584445/article/details/82259004