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方法:
復(fù)制代碼 代碼如下:
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"];
正則分析法:
方法一:
復(fù)制代碼 代碼如下:
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)用:
復(fù)制代碼 代碼如下:
alert(GetQueryString("參數(shù)名1"));
alert(GetQueryString("參數(shù)名2"));
alert(GetQueryString("參數(shù)名3"));
方法二:
復(fù)制代碼 代碼如下:
<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)用:
復(fù)制代碼 代碼如下:
<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)文章
javascript不同類型數(shù)據(jù)之間的運算的轉(zhuǎn)換方法
這篇文章主要介紹了javascript不同類型數(shù)據(jù)之間的運算的轉(zhuǎn)換方法,需要的朋友可以參考下2014-02-02
javascript Array.remove() 數(shù)組刪除
下面的代碼主要是實現(xiàn)了,刪除數(shù)組中指定的值。2009-08-08
js實現(xiàn)的四級左側(cè)網(wǎng)站分類菜單實例
這篇文章主要介紹了js實現(xiàn)的四級左側(cè)網(wǎng)站分類菜單,實例分析了javascript操作頁面元素實現(xiàn)tab切換的相關(guān)技巧,需要的朋友可以參考下2015-05-05
javascript中數(shù)組的sort()方法的使用介紹
數(shù)組的sort()方法用于排序,在本文將為大家介紹下javascript中sort()方法的具體使用2013-12-12

