Javascript获取url参数(queryString)一个比较简洁的方法
2008-10-23 17:24
931 查看
通过JavaScript获取url中的参数信息不是很困难,方法很多。之所以用这个方法,是觉得它利用了正则表达式,很简练。
function getQuery(name)
{
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
例如有个需求,在一个页面中用Javascript实现根据url参数ErrType的不同的值在页面中显示不同的错误信息:
<div id="divError" style="color:red;font-weight:bold;text-align:center;width:100%;height:300px;padding-top:30px;"></div>
<script language="javascript">
// define the descriptions of errors
var errInfos = { "1" : "The submitting user should not be allowed to review his submissions",
"2" : "Reviewers cannot review the same submission twice",
"3" : "Only people in the “reviewers” group should be able to access the review form"
}
function getQuery(name)
{
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
var errType = getQuery( "ErrType" )
if( errType )
document.getElementById("divError").innerText = errInfos[errType]
</script>
function getQuery(name)
{
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
例如有个需求,在一个页面中用Javascript实现根据url参数ErrType的不同的值在页面中显示不同的错误信息:
<div id="divError" style="color:red;font-weight:bold;text-align:center;width:100%;height:300px;padding-top:30px;"></div>
<script language="javascript">
// define the descriptions of errors
var errInfos = { "1" : "The submitting user should not be allowed to review his submissions",
"2" : "Reviewers cannot review the same submission twice",
"3" : "Only people in the “reviewers” group should be able to access the review form"
}
function getQuery(name)
{
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
var errType = getQuery( "ErrType" )
if( errType )
document.getElementById("divError").innerText = errInfos[errType]
</script>
相关文章推荐
- JavaScript获取URL中参数querystring的方法详解
- JavaScript:获取url的querystring参数
- 使用JavaScript获取URL中的参数(两种方法)
- javascript获取当前url网址及其参数方法总结
- javascript/jquery获取地址栏url参数的方法
- Javascript 获取链接(url)参数的方法
- javascript获取url中对应参数的方法
- 写一个方法获取 url ?后面的参数,并将参数对象化。
- [转载]javascript/jquery获取地址栏url参数的方法
- javascript/jquery获取地址栏url参数的方法
- JavaScript获取URL参数的方法之一
- JavaScript实现QueryString获取GET参数的方法
- Javascript 获取链接(url)参数的方法
- JavaScript实现QueryString获取GET参数的方法
- javascript获取url参数的方法
- 一个JS获取url参数的方法
- Javascript 获取链接(url)参数的方法
- javascript获取url参数通用方法
- Javascript 获取链接(url)参数的方法
- 使用JavaScript获取URL中的参数(两种方法)