JS_js直接获取地址栏参数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="" onclick="a()">获取</div>
	</body>
</html>
<script>
	function urldata(name){
     
     
		//构造一个含有目标参数的正则表达式对象
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		//匹配目标参数
		var r = window.location.search.substr(1).match(reg);
		//返回参数值
		if (r!=null) return unescape(r[2]);
		return null;
	}
	function a(){
     
     
		var b = urldata('id')			//获取地址栏的id参数,所有这里传入id,即b就是想要的参数,地址栏有id这个参数可获取才可以拿到,不然会报错
		console.log(b)
	}
</script>

猜你喜欢

转载自blog.csdn.net/weixin_44599931/article/details/108017736