Example of using Javascript to obtain the parameter transfer value of HTML static pages

Source: Internet
Author: User

Let's take a look at my code. Just embed the code into a page file.

Example 1
Use regular expressions to obtain
Copy codeThe Code is as follows:
Var LocString = string(registry.doc ument. location. href );
Function getQueryStr (str ){
Var rs = new RegExp ("(^ |)" + str + "= ([^ &] *) (& | $)", "gi" cmd.exe c (LocString ), tmp;
If (tmp = rs ){
Return tmp [2];
}
// Parameter cannot be found
Return "";
}

Call Method
Copy codeThe 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 data into arrays by Parameters
Copy codeThe Code is as follows:
<Script>
Urlinfo = window. location. href; // obtain the url of the current page
Len = urlinfo. length; // obtain the url length.
Offset = urlinfo. indexOf ("? "); // Set the start position of the parameter string
Newsidinfo = urlinfo. substr (offset, len) // retrieve the parameter string and obtain a string similar to "id = 1 ".
Newsids = newsidinfo. split ("="); // splits the obtained parameter string by "="
Newsid = newsids [1]; // obtain the parameter value.
Alert ("the parameter value you want to pass is" + newsid );
</Script>

However, you must remember that this method is only useful for URLs containing parameters. If the other party uses the post method to pass Parameters, the url does not contain parameters. Therefore, this technique is only useful for GET methods or URLs with specified parameters.

The following describes a complete instance.

Aa.htm is a parameter infiltration Interface
Bb.htm is the parameter receiving and processing interface.
Aa.htm
Copy codeThe Code is as follows:
<Html>
<Head>
</Head>
<Body>
<Script>
Function submit ()
{
Var input1 = document. getElementById ("inputid ");
Window. open ("bb.htm? InputStr = "+ input1.value); // input parameters
}
</Script>
<Input type = "text" id = "inputid">
<Input type = "button" onclick = "submit ()" value = "submit">
</Body>
</Html>
Bb.htm:
<Html>
<Head>
<Script>
// Method for obtaining Parameters
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>
// Obtain parameters by calling a method
Var rt = request. QueryString ("inputStr ");
Alert (rt );
</Script>
</Body>
</Html>

Bb.htm
Copy codeThe Code is as follows:
<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 ("");
Var B = request. QueryString ("B ");
Var c = request. QueryString ("c ");
If (! = Null) {a = a} else {a = "parameter A blank "}
If (B! = Null) {B = B} else {B = "parameter B blank "}
If (c! = Null) {c = c} else {c = "parameter C null "}
Document. writeln ("parameter A:" + );
Document. writeln ("<br> parameter B:" + B );
Document. writeln ("<br> parameter C:" + c );
// -->
</SCRIPT>
</Head>
<Body>
<Form name = "form1" action = "? ">
Enter the parameter value: <br>
<Script language = "JavaScript">
Document. writeln ("A: <input type = 'text' name = 'A' value = '" + a + "'> <br> ");
Document. writeln ("B: <input type = 'text' name = 'B' value = '" + B + "'> <br> ");
Document. writeln ("C: <input type = 'text' name = 'c' value = '" + c + "'> <br> ");
</SCRIPT>
<Input type = "submit" name = "Submit" value = "submit parameter query results">
</Form>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.