首页 > Web开发 > 详细

js获取get方式传递的参数

时间:2016-11-26 20:21:50      阅读:235      评论:0      收藏:0      [点我收藏+]
 String.prototype.GetValue= function(parm) {  
   var reg = new RegExp("(^|&)"+ parm +"=([^&]*)(&|$)");  
   var r = this.substr(this.indexOf("\?")+1).match(reg);  
   if (r!=null) return unescape(r[2]); return null;  
 }
完整测试代码
test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>


<script>
	String.prototype.getValue= function(parm) {  
		var reg = new RegExp("(^|&)"+ parm +"=([^&]*)(&|$)");  
		var r = this.substr(this.indexOf("\?")+1).match(reg);  
		if (r!=null) return unescape(r[2]); return null;  
	} 
	
	function init(){
		var url = window.location.href;
		if(url.getValue(‘key1‘) == null){
			alert(‘没有传递参数‘);
		}else{
			alert(‘传递参数:key1=‘ + url.getValue(‘key1‘));
			alert(‘传递参数:key2=‘ + url.getValue(‘key2‘));
		}
	}
</script>
</head>
<body onload="init();">
<input type="button" onclick="window.location=window.location + ‘?key1=value1&key2=value2‘;return false;" value="传递参数"/>
</body>
</html>

js获取get方式传递的参数

原文:http://www.cnblogs.com/yueyue-love/p/6104762.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!