JQ value acquisition method:
jquery itself does not have a method to obtain URL parameters, but there is already a plug-in, which can directly obtain URL and other parameters.
Plugin connection home page: https://github.com/allmarkedup/jQuery-URL-Parser
Download link: http://download.github.com/allmarkedup-jQuery-URL-Parser-bb2bf37.zip
Examples of use
Using the current page's url (for these examples https://mysite.com/information/about/index.html?itemID=2&user=dave):
// get the protocol
jQuery.url.attr("protocol") // returns 'http'
// get the path
jQuery.url.attr("path") // returns '/information/about/index.html'
// get the host
jQuery.url.attr("host") // returns 'mysite.com'
// get the value for the itemID query parameter
jQuery.url.param("itemID") // returns 2
// get the second segment from the url path
jQuery.url.segment(2) // returns 'about'
Using a different url to the current page:
// set a different URL and return the anchor string
jQuery.url.setUrl("http://allmarkedup.com/category/javascript/#footer").attr("anchor") // returns 'footer'
JS native access:
The most primitive JS method:
The code copy is as follows:
var URLParams = new Array();
var aParams = document.location.search.substr(1).split('&');
for (i=0; i < aParams.length ; i++){
var aParam = aParams[i].split('=');
URLParams[aParam[0]] = aParam[1];
}
Called like this:
http://127.0.0.1/index.php?name=name1&cid=123
//Get the passed name parameter
name=URLParams["name"];
document.write(name);
//Get the cid passed
cid=URLParams["cid"];
Regular analysis method:
Method 1:
The code copy 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;
}
Called like this:
The code copy is as follows:
alert(GetQueryString("parameter name 1"));
alert(GetQueryString("parameter name 2"));
alert(GetQueryString("parameter name 3"));
Method 2:
The code copy is as follows:
<span style="font-size: 16px;"><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;
}
</Script>
Called like this:
The code copy is as follows:
<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var parameter 1, parameter 2, parameter 3, parameter N;
Parameter 1 = Request['parameter 1'];
Parameter 2 = Request['parameter 2'];
Parameter 3 = Request['parameter 3'];
Parameter N = Request['parameter N'];
</Script>