码迷,mamicode.com
首页 > Web开发 > 详细

js获取get方式传递的参数

时间:2016-11-26 20:22:05      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:href   文档   pre   xmlns   text   var   xhtml   alert   click   

 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方式传递的参数

标签:href   文档   pre   xmlns   text   var   xhtml   alert   click   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!