Home > Web Front-end > JS Tutorial > js method to obtain (receive) address bar parameter value_javascript skills

js method to obtain (receive) address bar parameter value_javascript skills

WBOY
Release: 2016-05-16 17:38:39
Original
1003 people have browsed it

When the address bar contains parameters, we can use window.location.search on the target page to get the parameters and their values ​​we need

Copy code The code is as follows:

// Get the parameter array of the address bar
function getUrlParams()
{
var search = window.location.search;
// Write data dictionary
var tmparray = search.substr(1,search.length).split("&");
var paramsArray = new Array;
if( tmparray != null)
{
for(var i = 0;i{
var reg = /[=|^==]/; // Split with =, but not included ==
var set1 = tmparray[i].replace(reg,'&');
var tmpStr2 = set1.split('&');
var array = new Array;
array [tmpStr2[0]] = tmpStr2[1] ;
paramsArray.push(array);
}
}
// Return the parameter array
return paramsArray ;
}
// Get parameter value based on parameter name
function getParamValue(name)
{
var paramsArray = getUrlParams();
if(paramsArray != null)
{
for(var i = 0 ; i < paramsArray.length ; i )
{
for(var j in paramsArray[i] )
{
if( j == name )
{
return paramsArray[i][j] ;
}
}
}
}
return null ;
}

Currently we The page address is http://www.jb51.net/tatame/admin/EditPosts.aspx?opt=1
You can see that the url contains a parameter
var opt = getParamValue("opt");
alert(opt); pops up to 1.
Copy code The code is as follows:

/********
Receive address bar parameters
key: parameter name
*************/
function GetQuery(key) {
var search = location.search.slice(1); //Get the query string submitted by get method
var arr = search.split("&");
for ( var i = 0; i < arr.length; i ) {
var ar = arr[i].split("=");
if (ar[0] == key) {
return ar[1];
}
}
}

Just call the GetQuery('test') function when the page is loaded. It is very practical.
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