JS to get URL passing parameters)

Source: Internet
Author: User
JS get URL passing Parameters

Here is a javascript client solution for obtaining URL parameters with questring, equivalent to ASP's request. querystring, PHP's $ _ Get

Function:

<Script language = "JavaScript"> function getrequest () {var url = location. Search; // get URL "? "Var therequest = new object (); If (URL. indexof ("? ")! =-1) {var STR = URL. substr (1); STRs = Str. split ("&"); For (VAR I = 0; I <STRs. length; I ++) {therequest [STRs [I]. split ("=") [0] = (STRs [I]. split ("=") [1]) ;}return therequest ;}</SCRIPT>

Then, we can call this function to obtain the corresponding parameter values:

<Script language = "JavaScript">
VaR request = new object ();
Request = getrequest ();
VaR parameter 1, parameter 2, parameter 3, parameter n;
Parameter 1 = request [''parameter 1 ''];
Parameter 2 = request [''parameter 2''];
Parameter 3 = request [''parameter 3 ''];
Parameter n = request [''parameter n''];
</SCRIPT>
Obtain the parameter with the same name in the URL string.

Ii. Regular Expression Analysis.
Function getquerystring (name ){
VaR Reg = new
Regexp ("(^ | &)" + name +
"= ([^ &] *) (& | $)", "I ");
VaR r =
Window. Location. Search. substr (1). Match (REG );
If (R! = NULL) Return
(R [2]); return NULL;
}
Alert (getquerystring ("parameter name 1 "));
Alert (getquerystring ("parameter name 2 "));
Alert (getquerystring ("parameter name 3 "));

Other parameters:

// Set or obtain the specified file name or path of the object.
Alert (window. Location. pathname );

// Set or obtain the whole URL
Is a string.
Alert (window. Location. href );

// Set or obtain the URL
The associated port number.
Alert (window. Location. Port );

// Set or obtain a URL
.
Alert (window. Location. Protocol );

// Set or obtain href
Segment after the # In the attribute.
Alert (window. Location. Hash );

// Set or obtain the location or URL
Hostname and port number.
Alert (window. Location. HOST );

// Set or obtain href
The attribute following the question mark.
Alert (window. Location. Search );

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.