Js+Jq獲取URL參數(shù)的集中方法示例代碼
更新時間:2014年05月20日 17:43:06 作者:
這篇文章主要介紹了Js+Jq獲取URL參數(shù)的集中方法,需要的朋友可以參考下
JQ取值方法:
jquery本身也不存在取得URL參數(shù)的方法,但是已經(jīng)存在插件,可以直接取得URL等參數(shù)
插件連接主頁:https://github.com/allmarkedup/jQuery-URL-Parser
下載鏈接: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原生獲?。?
最原始的JS方法:
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];
}
這樣調(diào)用:
http://127.0.0.1/index.php?name=name1&cid=123
//取得傳過來的name參數(shù)
name=URLParams["name"];
document.write(name);
//取得傳過來的cid
cid=URLParams["cid"];
正則分析法:
方法一:
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;
}
這樣調(diào)用:
alert(GetQueryString("參數(shù)名1"));
alert(GetQueryString("參數(shù)名2"));
alert(GetQueryString("參數(shù)名3"));
方法二:
<span style="font-size: 16px;"><Script language="javascript">
function GetRequest() {
var url = location.search; //獲取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>
這樣調(diào)用:
<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var 參數(shù)1,參數(shù)2,參數(shù)3,參數(shù)N;
參數(shù)1 = Request['參數(shù)1'];
參數(shù)2 = Request['參數(shù)2'];
參數(shù)3 = Request['參數(shù)3'];
參數(shù)N = Request['參數(shù)N'];
</Script>
jquery本身也不存在取得URL參數(shù)的方法,但是已經(jīng)存在插件,可以直接取得URL等參數(shù)
插件連接主頁:https://github.com/allmarkedup/jQuery-URL-Parser
下載鏈接: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原生獲?。?
最原始的JS方法:
復制代碼 代碼如下:
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];
}
這樣調(diào)用:
http://127.0.0.1/index.php?name=name1&cid=123
//取得傳過來的name參數(shù)
name=URLParams["name"];
document.write(name);
//取得傳過來的cid
cid=URLParams["cid"];
正則分析法:
方法一:
復制代碼 代碼如下:
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;
}
這樣調(diào)用:
復制代碼 代碼如下:
alert(GetQueryString("參數(shù)名1"));
alert(GetQueryString("參數(shù)名2"));
alert(GetQueryString("參數(shù)名3"));
方法二:
復制代碼 代碼如下:
<span style="font-size: 16px;"><Script language="javascript">
function GetRequest() {
var url = location.search; //獲取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>
這樣調(diào)用:
復制代碼 代碼如下:
<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var 參數(shù)1,參數(shù)2,參數(shù)3,參數(shù)N;
參數(shù)1 = Request['參數(shù)1'];
參數(shù)2 = Request['參數(shù)2'];
參數(shù)3 = Request['參數(shù)3'];
參數(shù)N = Request['參數(shù)N'];
</Script>
相關(guān)文章
深入淺出webpack教程系列_安裝與基本打包用法和命令參數(shù)詳解
下面小編就為大家?guī)硪黄钊霚\出webpack教程系列_安裝與基本打包用法和命令參數(shù)詳解。小編覺得挺不錯的,現(xiàn)在就想給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09JS跨域之window.name實現(xiàn)的跨域數(shù)據(jù)傳輸
這篇文章主要介紹了JS跨域之window.name實現(xiàn)的跨域數(shù)據(jù)傳輸,需要的朋友可以參考下2022-01-01JavaScript樹形數(shù)據(jù)結(jié)構(gòu)處理
這篇文章主要介紹了JavaScript樹形數(shù)據(jù)結(jié)構(gòu)處理,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07