Javascript obtains the parameters passed by the URL in the address bar.

Source: Internet
Author: User
Sometimes we need to use URL parameters in the js file, but Javascript is the script language executed by the client, and Session is the object on the server, so we cannot directly obtain the value in the Session. So, how can we get URL parameters in the JS file? The following two methods are described... syntaxHighlighter. all (sometimes we need to use URL parameters in the js file, but Javascript is the script language executed by the client, and Session is the object on the server, so we cannot directly obtain the value in the Session. So, how can we get URL parameters in the JS file? The following describes two methods: String sharding and regular expression matching. First, the string splitting method is used to obtain the parameter section of the URL through the location. search method, and then further process it. The Code is as follows: [javascript] // obtain the function GetRequest () {// URL parameter passed in the address bar. Example: XXX. aspx? ID = "+ ID +" & Name = "+ Name; var url = location. search; // get url "? "And the subsequent string var theRequest = new Object (); if (url. indexOf ("? ")! =-1) // there is a question mark in the url, that is, there is a parameter. {Var str = url. substr (1); strs = str. split ("&"); for (var I = 0; I <strs. length; I ++) {theRequest [strs [I]. split ("=") [0] = unescape (strs [I]. split ("=") [1]) ;}return theRequest;} call the function code: [javascript] var Request = new Object (); // obtain Request = GetRequest (); var ID, Name; // ID, Name ID = Request ['id']; // ID Name = Request ['name']; // The second type of Name: Regular Expression matching is similar in principle to the previous method, which is extracted from the URL, the extraction method is different. [Javascript] function GetQueryString (name) {var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $) "); var r = window. location. search. substr (1 ). match (reg); if (r! = Null) {return unescape (r [2]);} return null;} call code: [javascript] var ID; ID = GetQueryString ("ID "); the preceding two methods are used to obtain URL parameters in Javascript.
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.