Home  >  Article  >  Web Front-end  >  Javascript Gets HTML Static Page Parameter Pass Value Example_javascript Tips

Javascript Gets HTML Static Page Parameter Pass Value Example_javascript Tips

WBOY
WBOYOriginal
2016-05-16 17:25:281189browse

Let me show you my code. Just embed these codes into the page file.

Example 1
Use regular expressions to get

Copy code The code is as follows:

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 "";
}

Call method
Copy code The code is as follows:

document.getElementById("user").value = getQueryStr( "user");
document.getElementById("password").value = getQueryStr("password");
document.getElementById("sysno").value = getQueryStr("sysno");

Example 2
Use the split function to cut into arrays according to parameters
Copy code The code is as follows:

<script> <br>urlinfo=window.location.href; //Get the url of the current page <br>len=urlinfo.length;//Get the url Length<br>offset=urlinfo.indexOf("?");//Set the starting position of the parameter string<br>newsidinfo=urlinfo.substr(offset,len)//Remove the parameter string and you will get something like "id= 1" such a string<br>newsids=newsidinfo.split("=");//Split the obtained parameter string according to "="<br>newsid=newsids[1];//Get the parameter value<br>alert("The parameter value you want to pass is " newsid); <br></script>

But be sure to remember that this method is only useful for urls that contain parameters. If the other party uses If you use the POST method to pass parameters, the URL will not contain parameters, so this technique is only useful for the GET method or the URL with specified parameters

Look at a complete example below

aa.htm It is the parameter input and infiltration interface
bb.htm is the parameter receiving and processing interface
aa.htm
Copy code The code is as follows:


()
 {
 var input1 = document.getElementById("inputid");
 window.open("bb.htm?inputStr=" input1.value); // Pass in parameters
 }
 
 
  
 
 bb.htm:
 
 
 <script> <br> // Method to obtain parameters <br> var request = <br> { <br> QueryString : function(val) <br> { <br> var uri = window.location.search; <br> var re = new RegExp("" val "=([^&?]*)", "ig"); <br> return ((uri.match(re))?(uri.match(re)[0].substr(val.leng th 1 )):null); <br> } <br> } <br> </script>
 
 
 <script> <br> The method gets the parameters <br> var rt = request.QueryString("inputStr"); <br> alert(rt); <br> </script>




bb.htm



Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn