javascript页面间传递参数

时间:2021-06-26 15:14:08

1.通过URL传递参数

传递参数页

      function setCity()

      {

          var str = document.getElementById("cityName");

          if (str.value == null || str.value == "") {

              alert('请输入到达城市!');

              return;

          }

          else{

               var url="5.html?cityName="+str.value;

               alert(url);

               var urlInfo=encodeURI(url);

               window.location=urlInfo;

          }

 }

<div class="main1of2" >到达城市:<input type="text" id="cityName"  /></div>

<input type="image" src="../images/TJ.png" onmousedown="setCity()" onclick="setCity()" style="border: 0px;"/>

接受参数页面

    function load()

    {

      var urlinfo = window.location.href;                                               //获取url

      var str = urlinfo.split("?")[1].split("=")[1];                                      

      //拆分url得到“=”号后面的值(先用split("?")[1]得到?号以后的值,再用split("=")[1]得到等号后面的值,split从0开始计数)

     var name = decodeURI(str);

   //decodeURI解码

     var imgUrl="../images/" + name + ".png";

     document.getElementById("imgBanci").src=imgUrl;

      }

<img id="imgBanci" />

2.通过剪贴板传递参数

传递参数页:

  function setCity()

      {

          var str = document.getElementById("cityName");

          if (str.value == null || str.value == "") {

              alert('请输入到达城市!');

              return;

          }

          else {

              clipboardData.clearData("Text");

                    //清空剪贴板中“Text“格式的所有内容

              clipboardData.setData("Text",str.value);

                    //设置剪贴板中“Text“格式的内容

              window.location = "5.html";

                    //页面跳转

          }

接受参数页

      function load()

      {

          var str = clipboardData.getData("Text");

          //访问剪贴板中的“Text“格式的内容

          var imgUrl = "../images/" + str + ".png";

          //生成图片路径

          document.getElementById("imgBanci").src = imgUrl;

      }