Javascript获取url参数值
生活随笔
收集整理的這篇文章主要介紹了
Javascript获取url参数值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天碰到要在一個頁面獲取另外一個頁面url傳過來的參數(shù),一開始很本能的想到了用 split("?")這樣一步步的分解出需要的參數(shù)。
后來想了一下,肯定會有更加簡單的方法的!所以在網(wǎng)上找到了兩個很又簡單實用的方法,mark下
方法一:正則分析法
?
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></span> |
這樣調(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>?
----------------------------------------------------------------------------------------------------------------------------------------
/*
獲取URL中最后一項參數(shù)的值
*/
var?str=window.location.href;
//alert(str);
var?es=/SouceID=/;?
es.exec(str);?
var?right=RegExp.rightContext;?
//alert(right);
???
? //列子
????<script language="javascript">?
????var str=window.location.href;?
????var es=/SouceID=/;?
????es.exec(str);?
????var right=RegExp.rightContext;?
????//alert(right);
????switch(right){
????case 'Din':
????case 'Exh':
????case 'Banks':
????case 'Shop':
????case 'Treat':
????case 'Trip':
?????????ChgTab('tab3','tabcontent3');
?????????break;
????case 'Air':
????case 'Railway':
????case 'Road':
????case 'Subway':
??????????ChgTab('tab2','tabcontent2');
??????????break;
???default:
?????????ChgTab('tab1','tabcontent1');
}
</script>?
//以下是函數(shù)的寫法
function?GetParam(){
????var?url?=?document.location.href;
????var?name=""
????if?(url.indexOf("=")>0)
????{
????????name?=?url.substring(url.indexOf("=")+1,url.length)
????}
????return?name;
}
/*
獲取指定的URL參數(shù)值
URL:http://www.blogjava.net/blog?name=bainian
參數(shù):paramName?URL參數(shù)
調(diào)用方法:getParam("name")
返回值:bainian
*/
//1.
function?getParam(paramName)
{
????????paramValue?=?"";
????????isFound?=?false;
????????if?(this.location.search.indexOf("?")?==?0?&&?this.location.search.indexOf("=")>1)
????????{
????????????arrSource?=?unescape(this.location.search).substring(1,this.location.search.length).split("&");
????????????i?=?0;
????????????while?(i?<?arrSource.length?&&?!isFound)
????????????{
????????????????if?(arrSource[i].indexOf("=")?>?0)
????????????????{
?????????????????????if?(arrSource[i].split("=")[0].toLowerCase()==paramName.toLowerCase())
?????????????????????{
????????????????????????paramValue?=?arrSource[i].split("=")[1];
????????????????????????isFound?=?true;
?????????????????????}
????????????????}
????????????????i++;
????????????}???
????????}
???return?paramValue;
}
//2.
function?Request(sName)
{
??/*
???get?last?loc.?of??
???right:?find?first?loc.?of?sName
???+2
???retrieve?value?before?next?&
??
??*/
??
??var?sURL?=?new?String(window.location);
??var?sURL?=?document.location.href;
??var?iQMark=?sURL.lastIndexOf('?');
??var?iLensName=sName.length;
??
??//retrieve?loc.?of?sName
??var?iStart?=?sURL.indexOf('?'?+?sName?+'=')?//limitation?1
??if?(iStart==-1)
????????{//not?found?at?start
????????iStart?=?sURL.indexOf('&'?+?sName?+'=')//limitation?1
????????????????if?(iStart==-1)
???????????????????{//not?found?at?end
????????????????????return?0;?//not?found
???????????????????}???
????????}
????????
??iStart?=?iStart?+?+?iLensName?+?2;
??var?iTemp=?sURL.indexOf('&',iStart);?//next?pair?start
??if?(iTemp?==-1)
????????????????{//EOF
????????????????iTemp=sURL.length;
????????????????}??
??return?sURL.slice(iStart,iTemp?)?;
??sURL=null;//destroy?String
}??
----------------------------------------------------------------------------------------------------------------------------------------?
?比如一條url:http://127.0.0.1/index.html?id=999&type=2那么在index.html中這樣寫就可以獲得id和type的值var id=$.query.get("id"); ?// id=999var type=$.query.get("type"); ?// type=2插件的代碼如下:http://blog.jiqila.com/post/164/?
轉(zhuǎn)載于:https://www.cnblogs.com/zagelover/articles/2756652.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Javascript获取url参数值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 11.05 scrum report
- 下一篇: 对PostgreSQL中 index o