JavaScript gets the parameters in the current URL

Source: Internet
Author: User

JavaScript gets the parameters in the current page URL can use the search method of location, get to the URL? The following sections, such as Http:localhost:8080/manager/index.jsp?id=1

Use the search method of location to get to the string? id=1; want to get? The following key-value pairs can be intercepted using the substring method, get id=1 after interception, obtain the value of the ID, you can split it using the Split () method, and split the expression to "=". Here's a look at the specific example:

Window.onload = function () {var param = location.search.substring (1); var results = param.split ("="); var result = Results[1 ];if (result== "-1") {alert ("id value is-1");} else if (result = = "1") {alert ("id value is 1");} else if (result = = "2") {alert ("ID value is 2");}};

If there are multiple parameters in the URL, that is, each parameter uses & to split, then we get to? The subsequent string can be split with split first, split the expression to &, and then get the array to split each string. To get each parameter.

JavaScript gets the parameters in the current URL

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.