js獲取url傳值的方法
本文實例講述了js獲取url傳值的方法。分享給大家供大家參考,具體如下:
js獲取url參數(shù)值:
index.htm?參數(shù)1=數(shù)值1&參數(shù)2=數(shù)值2&參數(shù)3=數(shù)據(jù)3&參數(shù)4=數(shù)值4&......
靜態(tài)html文件js讀取url參數(shù) 根據(jù)獲取html的參數(shù)值控制html頁面輸出
一、字符串分割分析法
這里是一個獲取URL帶QUESTRING參數(shù)的JAVASCRIPT客戶端解決方案,相當(dāng)于asp的request.querystring,PHP的$_GET
函數(shù):
<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)用此函數(shù)獲取對應(yīng)參數(shù)值:
<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ù)
二、正則分析法
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("參數(shù)名1"));
alert(GetQueryString("參數(shù)名2"));
alert(GetQueryString("參數(shù)名3"));
補(bǔ)充:js 獲取url的get傳值函數(shù)
function getvl(name) {
var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
if (reg.test(location.href))
return unescape(RegExp.$2.replace(/\+/g, " "));
return "";
};
用法簡單的說一下
比如url是:http://localhost/index.php?qz=ddddk中qz的值代碼如下
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
Javascript實現(xiàn)仿WebQQ界面的“浮云”兼容 IE7以上版本及FF
兼容:IE7以上版本及FF;(騰訊的WebQQ3.0好像也不兼容IE6,其實這樣挺好的)2011-04-04
TypeScript判斷兩個數(shù)組的內(nèi)容是否相等的實現(xiàn)
本文主要介紹了TypeScript?判斷兩個數(shù)組的內(nèi)容是否相等,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

