似月是你
God helps those who help themselves.
天 道 酬 勤

将对象拼接成地址,改地址栏url不刷新

 

function route(Url, param){
        let arr= [];
        for (let x in param) {
            if(param[x]){
                arr.push(x + '=' + param[x]);
            }
        }
        let url = Url;
        if (arr.length == 0) {
            return url.replace(/amp;/g,'');
        }
        if (url.indexOf('?') >= 0) {
            url += '&';
        } else {
            url += '?';
        }
        url += arr.join('&');
        return url.replace(/amp;/g,'');
 }

  function edit_url(href){
    var urls = document.URL, //获取当前页面的网址信息
    URL;
    var num = href.indexOf('#');//获取#在的位置信息
    if (num){
      URL = href.substring(0,num); //截取网址信息
      history.pushState(null,null,URL); //将网址设置
    }
  }


var href= route("http://baidu.com/",{arr1: "1",arr2: "2");
href = href + "#/a";
edit_url(href);

 

posted @ 2020-09-08 11:41  似月是你  阅读(250)  评论(0编辑  收藏  举报