Javascript shares a method to obtain the urlget parameter.

Source: Internet
Author: User
We often see that some url links are followed by parameters. Sometimes we need to obtain these url parameters for use in development. This article introduces a method to obtain the urlget parameter. For more information about the coders, see. This function uses the indexOf method and split method in javascript. The Code is as follows:

Function getPara (para) {if (location. href. indexOf ("? ") =-1) {// if there is no parameter, Do nothing. return null;} else {// GET request? Var urlQuery = location. href. split ("? "); If (urlQuery [1]. indexOf ("&") =-1) {// there is only one parameter if (urlQuery [1]. indexOf ("=") =-1) {// no equal sign, no parameter, Do nothing return null;} else {var keyValue = urlQuery [1]. split ("="); var key = keyValue [0]; var value = keyValue [1]; if (key = para) {return value ;}}} else {// resolution parameter var urlTerms = urlQuery [1]. split ("&"); for (var I = 0; I <urlTerms. length; I ++) {var keyValue = urlTerms [I]. split ("="); var key = keyValue [0]; var value = keyValue [1]; if (key = para) {return value ;}}}} return null ;}

You can directly call the getPara function to obtain the parameters of a url.

The js/html/php/css code in this article can be copied to this page for online debugging. You may wish to give it a try.

Http://www.manongjc.com/runcode

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.