当前位置 : 主页 > 网络编程 > JavaScript >

获取url后面的参数

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt export function getRequest() { var kstr,vstr; var url1=location.href; var url="?"+url1.split('?')[1] var url2=url.split('#')[0]/ar url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url2
gistfile1.txt
export function getRequest() {
  var kstr,vstr;
  var url1=location.href;
  var url="?"+url1.split('?')[1]
  var url2=url.split('#')[0]
/ar url = location.search; //获取url中"?"符后的字串
  var theRequest = new Object();
  if (url2.indexOf("?") != -1) {
    var str = url2.substr(1);
    var strs = str.split("&");
    for(var i = 0; i < strs.length; i ++) {
    	var idx=strs[i].indexOf('=')
    	if(idx>=0){
    		theRequest[strs[i].split("=")[0]]=unescape(strs[i].substr(idx+1));
    	}else{
    		
    	}
    }
  }
  return theRequest;
}


//第二种

common.getUrlParams = function(){
  var url = window.location.href   //获取url中“?”符后的字串
        //  var url = parent.document.getElementById('iframe').src
  var theRequest = {};
    if(url.indexOf('?')! = -1){
      var str = url.substr(url.indexOf('?')+1)
      var strs = str.split('&')
      for(var i = 0; i < str.length; i++ ){
        theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
      }
    }
    return theRequest
}
网友评论