Home > Web Front-end > JS Tutorial > Summary of several methods for obtaining address bar parameters in JS_javascript skills

Summary of several methods for obtaining address bar parameters in JS_javascript skills

WBOY
Release: 2016-05-16 16:57:51
Original
1024 people have browsed it

Copy code The code is as follows:

<script><br> // Get the address bar Parameter array<br> function getUrlParams() {<br> var search = window.location.search;<br> // Write data dictionary<br> var tmparray = search.substr(1, search.length).split ("&");<br> var paramsArray = new Array;<br> if (tmparray != null) {<br> for (var i = 0; i < tmparray.length; i ) {<BR> var reg = /[=|^==]/; // Split with =, but not including ==<BR> var set1 = tmparray[i].replace(reg, '&');<BR> var tmpStr2 = set1.split('&');<BR> var array = new Array;<BR> array[tmpStr2[0]] = tmpStr2[1];<BR> paramsArray.push(array);<BR> }<BR> }<BR> // Return the parameter array<BR> return paramsArray;<BR> }</P> <P> // Get parameter value based on parameter name <BR> function getParamValue(name) {<BR> var paramsArray = getUrlParams();<BR> if (paramsArray != null) {<BR> for (var i = 0 ; i < paramsArray.length; i ) {<BR> for (var j in paramsArray[i]) {<BR> if (j == name) {<BR> return paramsArray[i][j];<BR> }<BR> }<BR> }<BR> }<BR> return null;<BR> }</P> <P>function test()<BR>{<BR> alert("v=" getParamValue("name"));<BR>}</P> <P>var test1= function()<BR>{<BR> //alert("v1=" getParamValue("name"));<BR> //alert("v2=" GetUrlParam("name")) ;<BR> //alert("v3=" GetUrlParms("name"));<BR> alert("v4=" getQuery("name"));<BR>}</P> <P>function GetUrlParam(paramName) {<BR> var url = document.URL; //URL parameters, you can also use document.URL to get them, there are too many methods <BR> var oRegex = new RegExp('[? &]' paramName '=([^&] )', 'i');<BR> //var oMatch = oRegex.exec( window.top.location.search ); //Get the URL of the current window<BR> var oMatch = oRegex.exec(url);<BR> if (oMatch && oMatch.length > 1)<br> return oMatch[1]; //Return value<br> else<br> return '';<br>} <p>//Get url parameters<br>function GetUrlParms(name) {<br> var reg = new RegExp("(^|\?|&)" name "=([^&]*)(\s| &|$)", "i");<br> if (reg.test(location.href))<br> return unescape(RegExp.$2.replace(/ /g, " "));<br> return "";<br>}<br>var getQuery = function (i) {<br><br>var j = location.search.match(new RegExp("[?&]" i "=([^&] *)(&?)", "i"));<br><br>return j ? j[1] : j<br><br>};<br></script>

JS gets address bar parameters:

Get parameter value

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template