登录  /  注册
首页 > web前端 > js教程 > 正文

Javascript如何利用函数和正则表达式获取静态页面参数值实例详解

伊谢尔伦
发布: 2017-07-26 17:24:26
原创
1411人浏览过

获取html静态页面参数传递值可以利用split函数来按参数切成数组、利用正则表达式来获取,具体实现如下

例一  利用正则表达式来获取 

var LocString = String(window.document.location.href); 
function getQueryStr(str) { 
var rs = new RegExp("(^|)" + str + "=([^&]*)(&|$)", "gi").exec(LocString), tmp; 
if (tmp = rs) { 
return tmp[2]; 
} 
// parameter cannot be found 
return ""; 
}
登录后复制

调用方法

document.getElementById("user").value = getQueryStr("user"); 
document.getElementById("password").value = getQueryStr("password"); 
document.getElementById("sysno").value = getQueryStr("sysno");
登录后复制

例二 利用split函数来按参数切成数组

<script> 
urlinfo=window.location.href; //获取当前页面的url 
len=urlinfo.length;//获取url的长度 
offset=urlinfo.indexOf("?");//设置参数字符串开始的位置 
newsidinfo=urlinfo.substr(offset,len)//取出参数字符串 这里会获得类似“id=1”这样的字符串 
newsids=newsidinfo.split("=");//对获得的参数字符串按照“=”进行分割 
newsid=newsids[1];//得到参数值 
alert("您要传递的参数值是"+newsid); 
</script>
登录后复制

不过一定要记得 这个方法只是针对含有参数的url有用 ,如果对方用了POST方法传递参数, url中是不会含有参数的所以这个技巧只对GET方法或者指定了参数的url有用哦

下面看一个完整的实例
aa.htm是参数输渗入渗出界面
bb.htm是参数接收处理界面
aa.htm

 <html> 
  <head> 
  </head> 
  <body> 
  <script> 
  function submit() 
  { 
  var input1 = document.getElementById("inputid"); 
  window.open("bb.htm?inputStr=" + input1.value);//传入参数 
  } 
  </script> 
  <input type = "text" id = "inputid"> 
  <input type = "button" onclick = "submit()" value = "提交"> 
  </body> 
  </html> 
  bb.htm: 
  <html> 
  <head> 
  <script> 
  //获得参数的方法 
  var request = 
  { 
  QueryString : function(val) 
  { 
  var uri = window.location.search; 
  var re = new RegExp("" +val+ "=([^&?]*)", "ig"); 
  return ((uri.match(re))?(uri.match(re)[0].substr(val.leng th+1)):null); 
  } 
  } 
  </script> 
  </head> 
  <body> 
  <script> 
  //调用方法获得参数 
  var rt = request.QueryString("inputStr"); 
  alert(rt); 
  </script> 
  </body> 
  </html>
登录后复制

bb.htm

<html> 
  <head> 
  <title>test</title> 
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  <SCRIPT LANGUAGE="JavaScript"> 
  <!-- 
  var request = { 
  QueryString : function(val) { 
  var uri = window.location.search; 
  var re = new RegExp("" +val+ "=([^&?]*)", "ig"); 
  return ((uri.match(re))?(uri.match(re)[0].substr(val.leng th+1)):null); 
  } 
  } 
  var a = request.QueryString ("a"); 
  var b = request.QueryString ("b"); 
  var c = request.QueryString ("c"); 
  if ((a != null)){a=a} else{a="参数A空"} 
  if ((b != null)){b=b} else{b="参数B空"} 
  if ((c != null)){c=c} else{c="参数C空"} 
  document.writeln("参数A: " + a); 
  document.writeln("<br>参数B: " + b); 
  document.writeln("<br>参数C: " + c); 
  //--> 
  </SCRIPT> 
  </head> 
  <body> 
  <form name="form1" action="?"> 
  请输入参数值:<br> 
  <SCRIPT LANGUAGE="JavaScript"> 
  document.writeln("A:<input type=&#39;text&#39; name=&#39;a&#39; value=&#39;"+a+"&#39;><br>"); 
  document.writeln("B:<input type=&#39;text&#39; name=&#39;b&#39; value=&#39;"+b+"&#39;><br>"); 
  document.writeln("C:<input type=&#39;text&#39; name=&#39;c&#39; value=&#39;"+c+"&#39;><br>"); 
  </SCRIPT> 
  <input type="submit" name="Submit" value="提交参数查观效果"> 
  </form> 
  </body> 
  </html>
登录后复制


以上就是Javascript如何利用函数和正则表达式获取静态页面参数值实例详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号