Js+Jq to get the URL parameters of the centralized method sample code

  • 2020-03-30 03:01:45
  • OfStack

JQ value method:

Jquery itself does not exist to get URL parameters, but there is a plug-in, you can directly get URL and other parameters
Plug-in connection home page: https://github.com/allmarkedup/jQuery-URL-Parser
Download link: http://download.github.com/allmarkedup-jQuery-URL-Parser-bb2bf37.zip

Examples of the use
Using the current page 's url (for these examples at 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:
 
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]; 
} 

Call:

http://127.0.0.1/index.php? Name = name1 & cid = 123
// gets the passed name parameter
Name = URLParams (" name "),

Document. The write (name);
// get the transmitted cid

Cid = URLParams (" cid ");

Regular analysis:

Method one:
 
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; 
} 


Call:
 
alert(GetQueryString(" Parameter names 1")); 

alert(GetQueryString(" Parameter names 2")); 

alert(GetQueryString(" Parameter names 3")); 

Method 2:
 
<span style="font-size: 16px;"><Script language="javascript"> 
function GetRequest() { 
var url = location.search; //Get the "?" in the url String after a string
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> 

Call:
 
<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> 

Related articles: