Home  >  Article  >  Web Front-end  >  Js Jq centralized method sample code for obtaining URL parameters_javascript skills

Js Jq centralized method sample code for obtaining URL parameters_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:47:381398browse

JQ value method:

jquery itself does not have a method to obtain URL parameters, but there is already a plug-in, and you can directly obtain URL and other parameters
Plug-in connection homepage: 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 acquisition:

The most original JS method:

Copy Code The code 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];
}

Call 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 passed cid

cid= URLParams["cid"];

Regular analysis method:

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


is called like this:
Copy code The code is as follows:

alert(GetQueryString("Parameter name 1"));

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

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

Method 2:
Copy code The code is as follows:



Call like this:
Copy the code The code is as follows:


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