Loading

javascript页面间传递参数

1.通过URL传递参数

传递参数页

 1      function setCity()
 2 
 3      {
 4 
 5          var str = document.getElementById("cityName");
 6 
 7          if (str.value == null || str.value == "") {
 8 
 9              alert('请输入到达城市!');
10 
11              return;
12 
13          }
14 
15          else{
16 
17               var url="5.html?cityName="+str.value;
18 
19               alert(url);
20 
21               var urlInfo=encodeURI(url);
22 
23               window.location=urlInfo;
24 
25          }
26 
27 }

 

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

 

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

接受参数页面

 1    function load()
 2 
 3    {
 4 
 5      var urlinfo = window.location.href;                                               //获取url
 6 
 7      var str = urlinfo.split("?")[1].split("=")[1];                                      
 8 
 9      //拆分url得到“=”号后面的值(先用split("?")[1]得到?号以后的值,再用split("=")[1]得到等号后面的值,split从0开始计数)
10 
11     var name = decodeURI(str);
12 
13   //decodeURI解码
14 
15     var imgUrl="../images/" + name + ".png";
16 
17     document.getElementById("imgBanci").src=imgUrl;
18 
19      }

 

<img id="imgBanci" />

2.通过剪贴板传递参数

     传递参数页:

    

 1  function setCity()
 2 
 3      {
 4 
 5          var str = document.getElementById("cityName");
 6 
 7          if (str.value == null || str.value == "") {
 8 
 9              alert('请输入到达城市!');
10 
11              return;
12 
13          }
14 
15          else {
16 
17              clipboardData.clearData("Text");
18 
19                    //清空剪贴板中“Text“格式的所有内容
20 
21              clipboardData.setData("Text",str.value);
22 
23                    //设置剪贴板中“Text“格式的内容
24 
25              window.location = "5.html";
26 
27                    //页面跳转
28 
29          }

 

     接受参数页

 1      function load()
 2 
 3      {
 4 
 5          var str = clipboardData.getData("Text");
 6 
 7          //访问剪贴板中的“Text“格式的内容
 8 
 9          var imgUrl = "../images/" + str + ".png";
10 
11          //生成图片路径
12 
13          document.getElementById("imgBanci").src = imgUrl;
14 
15      }

 

posted @ 2014-03-11 20:49  WeihanLi  阅读(1865)  评论(0编辑  收藏  举报