日期:2014-05-16  浏览次数:20373 次

js获取get方式传递的参数
 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>