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