当前位置:编程学习 > JAVA >>

Javascript 获取Request请求参数实现

[javascript] 
String.prototype.getParameter = function (key) { 
    var re = new RegExp(key + '=([^&]*)(?:&)?'); 
    return this.match(re) && this.match(re)[1]; 
}; 

测试代码:
[javascript]
<script type="text/javascript"> 
<!-- 
String.prototype.getParameter = function (key) { 
    var re = new RegExp(key + '=([^&]*)(?:&)?'); 
    return this.match(re) && this.match(re)[1]; 
};  www.zzzyk.com
 
var s = "http://www.baidu.com/index.html?x0=0&x1=1&x2=2&x3=3&x4=http://www.google.com"; 
document.write(s.getParameter('x0') + "<br/>"); 
document.write(s.getParameter('x1') + "<br/>"); 
document.write(s.getParameter('x2') + "<br/>"); 
document.write(s.getParameter('x3') + "<br/>"); 
document.write(s.getParameter('x4') + "<br/>"); 
document.write(s.getParameter('undefined') + "<br/>"); 
//--> 
</script> 

输出:
 0
1
2
3
 

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,