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 xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>javascript获取url参数和script标签中获取url参数</title> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> <script type="text/javascript" src="js1.js?a=abc&b=汉字&c=123"></script> <script type="text/javascript"> //lastest: url paramter: //测试链接:<a href="2.html?name=geovindu&sex=woman&age=12">test getQueryString</a> var queryStrings=function() {//get url querystring var params=document.location.search,reg=/(?:^\?|&)(.*?)=(.*?)(?=&|$)/g,temp,args={}; while((temp=reg.exec(params))!=null) args[temp[1]]=decodeURIComponent(temp[2]); return args; }; //只取一个: var queryString=function(key){ return (document.location.search.match(new RegExp("(?:^\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1]; } </script> </head> <body> <div id="urlparamter"></div> <div id="all"></div> <div id="scriptparamter" name="scriptparamter"></div> <div id="allscript"></div> <input type="text" id="txtput" value=""/> <script type="text/javascript"> var args=queryStrings(); document.getElementById("all").innerHTML=args.name+" | "+args.sex+" | "+args.age+",all"; //alert(args.name+" | "+args.sex+" | "+args.age); var argsname=queryString("name"); var argssex=queryString("sex"); var argsage=queryString("age"); document.getElementById("urlparamter").innerHTML =argsname+","+argssex+","+argsage; //document.getElementById("txtput").value="geo"; //$("#scriptparamter")[0].innerHTML="ges"; </script> </body> </html>
js1.js:
/*http://css.dusystem.com/example/index.html window.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-javascript http://www.cnblogs.com/sohighthesky/archive/2010/01/21/1653126.html http://www.codeproject.com/Tips/574956/How-to-get-URL-and-QueryString-value-in-an-ASP-NET test: <a href="1.html?id=5&type=1&flag=0" title="d">test</a> */ var strget="du"; function getArgs(strParame) { var args = new Object( ); var query = location.search.substring(1); // Get query string var pairs = query.split("&"); // Break at ampersand for(var i = 0; i < pairs.length; i++) { var pos = pairs[i].indexOf('='); // Look for "name=value" if (pos == -1) continue; // If not found, skip var argname = pairs[i].substring(0,pos); // Extract the name var value = pairs[i].substring(pos+1); // Extract the value value = decodeURIComponent(value); // Decode it, if needed args[argname] = value; // Store as a property } return args[strParame]; // Return the object } //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; strget=argsscript.a; //alert(argsscript.a+" | "+argsscript.b+" | "+argsscript.c+","+strget); //document.getElementById("txtput").value=strget; //document.getElementById("scriptparamter").innerHTML ="ge"; //txtput //假如上面的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")); //set value window.onload = function () { getCommunitys(strget); } function getCommunitys(str) { //document.getElementById("scriptparamter").innerHTML =str; $("#scriptparamter")[0].innerHTML="ges"; document.getElementById("txtput").value=str; }
时间: 2024-10-12 10:45:42