两个静态HTML互相传值

关键词 :url

主要思想: 通过url 在两个页面之间传值

1、 a.html

<!DOCTYPE html>
<html lang="en">
<head>    
  <meta charset="UTF-8">    
  <title>a</title>
 </head>
<body>
  <input type="text" id="name" class="input7">
  <input type="button" value="OK" onclick="show()"/>

<script>    
  function show(){        
    var result = document.getElementById("name").value;        
    location.href="b.html?name="+result;      //利用url参数传递!!!
}
</script>
</body>
</html>

2、b.html

<!DOCTYPE html>
<html lang="en">
<head>    
<meta charset="UTF-8">    
<title>b</title>
</head>
<body>    
  <input type="button" onclick="getValue('name')" value="GetValue"/>
<script>    
function getValue(name) {        
  var str = window.location.search;   // location.search是从当前URL的?号开始的字符串        
  if (str.indexOf(name) != -1) {            
      var pos_start = str.indexOf(name) + name.length + 1;            
      var pos_end = str.indexOf("&", pos_start);            //检查是否有其他的参数传递
      if (pos_end == -1) {                
          alert(str.substring(pos_start));            
      } else {         
         alert("没有此值~~");            
      }        
  }    
}
</script>
</body>
</html>

3、效果图

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,571评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,666评论 25 708
  • 南直隶扬州府,在我尚未出生之时,父亲和在扬州经营买卖的苏州洞庭商人江春共同创办了江锦绣场。 据说江家先辈乃苏州...
    飞狐三爷阅读 441评论 0 0
  • 人总是在被时间这双巨手推着走,一步也不能停。小时候总幻想自己是一条鱼,这个世界是一眼望不到头的蓝色大海。我像个幽灵...
    云外花阅读 461评论 1 2