經常使用的url轉對象的手寫方法(未完成,待續)

1. window.location.search 轉 query對象

  • localhost:3000/article.html?_id=5ce8e25120517f3a68a5b060 中的
  • ?_id=5ce8e25120517f3a68a5b060 就是 window.location.search
function searchToQuery(search) {
        search = search || window.location.search;
        var queryObj = {};
        if (search.indexOf('?') == 0) {
            search = search.substr(1);
        }
        var query = search.split('=')
        for (var i = 0; i < query.length; i += 2) {
            queryObj[query[i]] = query[++i]
        }
        return queryObj;
    }

複製代碼
相關文章
相關標籤/搜索