javascript获取当前url中的參数

javascript获取当前页面url中的參数能够使用location的search方法,获取到的是url中?后面的部分,比如http:localhost:8080/Manager/index.jsp?

id=1

使用location的search方法能够获取到字符串?id=1;想要获取?后面的键值对能够使用substring方法对其进行截取,截取后获得id=1;须要获得id的值,能够使用split()方法对其进行拆分,拆分表达式为“=”。以下看详细样例:

window.onload = function(){
			var param = location.search.substring(1);
			var results = param.split("=");
			var result = results[1];
			if(result=="-1"){
				alert("id值为-1");
			}else if(result =="1"){
				alert("id值为1");
			}else if(result =="2"){
				alert("id值为2");
			}
		};

假设在url中有多个參数。即每个參数中使用&进行切割,这时我们获取到?后面的字符串时能够先用split进行拆分。拆分表达式为&,然后得到数组中对每个字符串对其进行切割。就可以获取每个參数。

posted @ 2016-01-17 17:46  mengfanrong  阅读(175)  评论(0编辑  收藏  举报