最近做个页面,需要用javascript获取url参数,可发现网上的东西,实在是多了,错误也实在是多啊,真晕,浪费了我不少时间于是,我便总结一下,一个好用的就可以了,先声明下,用正则,是简单,但多浏览器的兼容性,和速度不敢保证了,所以我就用个多浏览器兼容的
显示代码打印
01 <script language = javascript>??
02 function request(paras){??
03 var url = location.href;???
04 var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");???
05 var paraObj = {}???
06 for (i=0; j=paraString[i]; i++){???
07 paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);???
08 }???
09 var returnValue = paraObj[paras.toLowerCase()];???
10 if(typeof(returnValue)=="undefined"){???
11 return "";???
12 }else{???
13 return returnValue;???
14 }???
15 }??
16 var theurl=request('url');??
17 var theimg=request('img');??
18 document.writeln("<a href='"? + theurl + "' target=_blank><img src=" + theimg + " width=750 height=450 border=0></a>");??
19 </script>
?
?
最主要的是用到了
显示代码打印
01 function request(paras){??
02 var url = location.href;???
03 var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");???
04 var paraObj = {}???
05 for (i=0; j=paraString[i]; i++){???
06 paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);???
07 }???
08 var returnValue = paraObj[paras.toLowerCase()];???
09 if(typeof(returnValue)=="undefined"){???
10 return "";???
11 }else{???
12 return returnValue;???
13 }???
14 }
文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/1_web/javascript/jsjs/20071031/81512.html