復制代碼 代碼如下:
String.prototype.getParameter = function (key) {
var re = new RegExp(key + '=([^&]*)(?:&)?');
return this.match(re) && this.match(re)[1];
};
測試代碼:
復制代碼 代碼如下:
<script type="text/javascript">
<!--
String.prototype.getParameter = function (key) {
var re = new RegExp(key + '=([^&]*)(?:&)?');
return this.match(re) && this.match(re)[1];
};
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