你的位置:首页 > Java教程

[Java教程]How to get the query string by javascript?


1.html:

<a href="2.html?name=geovindu&sex=woman&age=12">test getQueryString</a>

2.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html 

  js1.js:

/*http://css.dusystem.com/example/index.htmlwindow.location.protocol = "http:"window.location.host = "css.dusystem.com"window.location.pathname = "example/index.html"http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascripthttp://www.cnblogs.com/sohighthesky/archive/2010/01/21/1653126.htmlhttp://www.codeproject.com/Tips/574956/How-to-get-URL-and-QueryString-value-in-an-ASP-NETtest: <a href="1.html?id=5&type=1&flag=0" title="d">test</a>*///script paramter:var getScriptArgs=function(){//获取多个参数  var scripts=document.getElementsByTagName("script"),  script=scripts[scripts.length-1],//因为当前dom加载时后面的script标签还未加载,所以最后一个就是当前的script  src=script.src,  reg=/(?:\?|&)(.*?)=(.*?)(?=&|$)/g,  temp,res={};  while((temp=reg.exec(src))!=null) res[temp[1]]=decodeURIComponent(temp[2]);  return res;};//var argsscript=getScriptArgs();//document.getElementById("scriptparamter").innerHTML =argsscript.a+" | "+argsscript.b+" | "+argsscript.c;alert(argsscript.a+" | "+argsscript.b+" | "+argsscript.c);//假如上面的js是在这个js1.js的脚本中<script type="text/javascript" src="js1.js?a=abc&b=汉字&c=123//"><//script>var getScriptArg=function(key){//获取单个参数  var scripts=document.getElementsByTagName("script"),  script=scripts[scripts.length-1],  src=script.src;  return (src.match(new RegExp("(?:^\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1];};alert(getScriptArg("c"));