js获取url参数值的两种方式

js获取url参数值的两种方式
一个参数:

	var test =window.location.href;
    var 参数=test.split("?参数=")[1];

多个参数:
方式一:

    function GetQueryString(name) { 
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
      var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
      var context = ""; 
      if (r != null) 
         context = r[2]; 
      reg = null; 
      r = null; 
      return context == null || context == "" || context == "undefined" ? "" : context; 
    }

调用方法:

    var 参数1 = GetQueryString['参数1'];
	var 参数2 = GetQueryString['参数2'];
	var 参数3 = GetQueryString['参数3'];

方式二:

	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 = new Object();
	Request = GetRequest();
	var 参数1,参数2,参数3,参数N;
	参数1 = Request['参数1'];
	参数2 = Request['参数2'];
	参数3 = Request['参数3'];
	参数N = Request['参数N'];

你可能感兴趣的:(jquery)