日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Javascript获取url参数值

發布時間:2025/3/21 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Javascript获取url参数值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天碰到要在一個頁面獲取另外一個頁面url傳過來的參數,一開始很本能的想到了用 split("?")這樣一步步的分解出需要的參數。

后來想了一下,肯定會有更加簡單的方法的!所以在網上找到了兩個很又簡單實用的方法,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;
}

這樣調用:

alert(GetQueryString("參數名1"));
alert(GetQueryString(
"參數名2"));
alert(GetQueryString(
"參數名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>

這樣調用:


<
Script language="javascript">var?Request?=?new?Object();
Request?
=?GetRequest();
var?參數1,參數2,參數3,參數N;
參數1?
=?Request['參數1'];
參數2?
=?Request['參數2'];
參數3?
=?Request['參數3'];
參數N?
=?Request['參數N'];
</Script>?

----------------------------------------------------------------------------------------------------------------------------------------
/*
獲取URL中最后一項參數的值
*/


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>?


//以下是函數的寫法
function?GetParam(){
????
var?url?=?document.location.href;
????
var?name=""
????
if?(url.indexOf("=")>0)
????
{
????????name?
=?url.substring(url.indexOf("=")+1,url.length)
????}

????
return?name;
}



/*
獲取指定的URL參數值
URL:http://www.blogjava.net/blog?name=bainian
參數:paramName?URL參數
調用方法: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/?

轉載于:https://www.cnblogs.com/zagelover/articles/2756652.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Javascript获取url参数值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。