Js+Jq獲取URL參數(shù)的集中方法示例代碼
更新時(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)文章
利用momentJs做一個(gè)倒計(jì)時(shí)組件(實(shí)例代碼)
這篇文章主要介紹了利用momentJs做一個(gè)倒計(jì)時(shí)組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12深入淺出webpack教程系列_安裝與基本打包用法和命令參數(shù)詳解
下面小編就為大家?guī)硪黄钊霚\出webpack教程系列_安裝與基本打包用法和命令參數(shù)詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09javascript 獲取模態(tài)窗口的滾動(dòng)位置代碼
模態(tài)窗口的滾動(dòng)位置獲取辦法還有不知道的嗎?下面的方法或許對(duì)大家有所幫助,感興趣的朋友可以了解下,希望對(duì)大家有所幫助2013-08-08JS跨域之window.name實(shí)現(xiàn)的跨域數(shù)據(jù)傳輸
這篇文章主要介紹了JS跨域之window.name實(shí)現(xiàn)的跨域數(shù)據(jù)傳輸,需要的朋友可以參考下2022-01-01JS實(shí)現(xiàn)的表格行鼠標(biāo)點(diǎn)擊高亮效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)的表格行鼠標(biāo)點(diǎn)擊高亮效果代碼,涉及JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)變換頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11JS運(yùn)動(dòng)基礎(chǔ)框架實(shí)例分析
這篇文章主要介紹了JS運(yùn)動(dòng)基礎(chǔ)框架,實(shí)例分析了javascript定時(shí)器及div樣式的使用技巧,需要的朋友可以參考下2015-03-03JavaScript樹形數(shù)據(jù)結(jié)構(gòu)處理
這篇文章主要介紹了JavaScript樹形數(shù)據(jù)結(jié)構(gòu)處理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07