location.search
2011-06-23 09:40 爱研究源码的javaer 阅读(362) 评论(0) 编辑 收藏 举报设置或获取 网页地址跟在问号后面的部分
当以get方式在url中传递了请求参数时,可以利用location的search属性提取参数的值,下面的代码把参数的名称和对应的值存储在2个数组中。
<script>
function test()
{
var url=window.location.search;
if(url.indexOf("?")!=-1)
{
var str = url.substr(1)
strs = str.split("&");
var key=new Array(strs.length);
var value=new Array(strs.length);
for(i=0;i<strs.length;i++)
{
key[i]=strs[i].split("=")[0]
value[i]=unescape(strs[i].split("=")[1]);
alert(key[i]+"="+value[i]);
}
}
}
</script>
--------------------------------------------------------------
<html>
<head><title>none</title>
<script>
function getParam(name){
var params=location.search.substring(1).toLowerCase();alert('params='+params);
var paramList=[];
var param=null;
var parami;
if(params.length>0) {
if(params.indexOf("&") >=0) {
paramList=params.split( "&" );
}else {
paramList[0] = params;
}
for(var i=0,listLength = paramList.length;i<listLength;i++) {
parami = paramList[i].indexOf(name+"=" );
if(parami>=0) {
param =paramList[i].substr(parami+(name+"=").length);
}
}
}
return param;
}
function check(){
id=document.getElementById('test').value;alert('one'+id);
var userid=getParam(id);alert('two'+userid);
var flag=false;
var
str='2010001,2010002,2010003,2010004,2010005,2010006,2010007,2010008,2010009,2010010,2010011,2010012,2010013,2010014,2010015,2010016,2010017,2010018,2010019,2010020,2010021,2010022,2010023,2010024,2010025,2010026,2010027,2010028,2010029,2010030,2010031,2010032,2010033,2010034,2010035,2010036,2010037,2010038,2010039,2010040,2010041,2010042,2010043,2010044,2010045,2010046,2010047,2010048,2010049,2010050,2010051,2010052,2010053,2010054,2010055,2010056,2010057,2010058,2010059,2010060,2010061,2010062,2010063,2010064,2010065,2010066,2010067,2010068,2010069,2010070,2010071,2010072,2010073,2010074,2010075,2010076,2010077,2010078,2010079,2010080,2010081,2010082,2010083,2010084,2010085,2010086,2010087,2010088,2010089,2010090,2010091,2010092,2010093,2010094,2010095,2010096,2010097,2010098,2010099';
var arr=str.split(',');
for(var i=0,l=arr.length;i<l;i++){
if(userid == arr[i])
{
flag=true;
break;
}
}
if(!flag){
alert('demine!');
return false;
}else{
alert('right');
}
}
</script>
</head>
<body>
<table >
<form>
请输入<input type="text" name="test" id="test"/>
<input type="button" value="确认" onclick="javascript:check();"/>
</form>
</table>
</body>
</html>