js获取链接?后边的参数名称或者值

1.获取后边的参数名称<script type="text/javascript">

			var url = location.search; //获取url中"?"符后的字串 ,包括?
			console.log(url);

			function GetRequest() {
				var url = location.search; //获取url中"?"符后的字串 
				
				var theRequest = new Object();
				if(url.indexOf("?") != -1) {
					var str = url.substr(1);
					strs = str.split("&");
					for(var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
					}
				}
				return theRequest;
			}
			var Request = undefined;
			Request = GetRequest();
			console.log(Request);//最后获取的字段名+值
			
			const keys= Object.keys(Request);
			console.log(keys);//数组
			console.log(keys[0]);//输出a
		
		</script>

  

 

 

 2.获取?后边参数值:

function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

//例子
  var contest_id = getQueryString("contest_id");

  

 

posted @ 2019-09-03 17:10  陌凌雪  阅读(3473)  评论(0编辑  收藏  举报