Home > Web Front-end > JS Tutorial > body text

Two ways to get url parameter value in js_javascript skills

WBOY
Release: 2016-05-16 17:23:02
Original
1385 people have browsed it
Method 1: Regular Analysis
Copy the code The code is as follows:

function getQueryString(name) {
var reg = new RegExp("(^|&)" name "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

Calling method:
alert(GetQueryString("Parameter name 1")); alert(GetQueryString("Parameter name 2"));
alert(GetQueryString("Parameter name 3"));

Method 2
Copy code The code is as follows:

< Script language="javascript">
function GetRequest() {
var url = location.search; //Get the string after the "?" character in the url
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i ) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=") [1]);
}
}
return theRequest;
}


Calling method:
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