当前位置:首页 > 开发 > Web前端 > 前端 > 正文

javascritp添加url参数方法

发表于: 2014-09-23   作者:ling凌yue月   来源:转载   浏览:
摘要:     //将对象参数加入到url中,如果原来url中有则覆盖 function addToUrl(obj){ var aprotocol = location.protocol; var ahost = location.host; var apath = location.pathname; var asearch = location.sea

 

 

//将对象参数加入到url中,如果原来url中有则覆盖
function addToUrl(obj){
  var aprotocol = location.protocol;
  var ahost = location.host;
  var apath = location.pathname;
  var asearch = location.search;
  var ahash = location.hash;
  var result = '';
  console.log(obj);
  var joinObj = function(joinObj_obj){
    var result = '';
    for(var i in joinObj_obj){
      result += i + '=' + joinObj_obj[i];
    }
    return result;
  };
  var splitSearchToObj = function(str){
    var resObj = {};
    var arr = str.split('&');
    for(var i = 0; i < arr.length; i++){
      resObj[arr[0]] = arr[1];
    }
    return resObj;
  };
  var existObjKey = function(existObjKey_obj, str){
    for(var i in existObjKey_obj){
      if(i == str){
        return true;
      }
    }
    return false;
  };
  var objExtend = function(obj1, obj2){
    var result = {};
    for(var i in obj1){
      if(existObjKey(obj2, i)){
        result[i] = obj2[i];
      }else{
        result[i] = obj1[i];
      }
    }
  };
  if(asearch == ''){
    console.log(obj);
    result = aprotocol + '//' + ahost + apath + '?' + joinObj(obj) + ahash;
  }else{
    var oldSearchObj = splitSearchToObj(asearch.substr(1));
    result = aprotocol + '//' + ahost + apath + joinObj(objExtend(oldSearchObj, obj)) + ahash;
    
  }
  return result;
}

addToUrl({'kd': 'aaa'});

 

javascritp添加url参数方法

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号