經常使用URL參數操做方法

獲取單個參數

/** * [getParam ] * @param {String} name * @param {String} url [default:location.href] * @return {String|Boolean} */
function getParam(name, url) {
    if(typeof name !== 'string') return false;
    if (!url) url = window.location.href;
    // 當遇到name[xx]時,對方括號作一下轉義爲 name\[xxx\],由於下面還須要使用name作正則
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
    var results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

getParam('query','https://juejin.im/search?query=hello&time=2017-11-12')
// output: 
// "hello"複製代碼

設置單個參數

/** * [setParam 設置單個參數] * @param {String} name * @param {String|Number} val * @return {String|Boolean} */
function setParam(name, val, url) {
    if(typeof name !== 'string') return false;
    if (!url) url = window.location.href;
    var _name = name.replace(/[\[\]]/g, '\\$&');
    var value = name + '=' + encodeURIComponent(val);
    var regex = new RegExp(_name + '=[^&]*');
    var urlArr = url.split('#');
    var result = '';

    if(regex.exec(url)){
        result =  url.replace(regex, value);
    }else{
        result = urlArr[0]+'&'+value+ (urlArr[1] || '');
    }

    return result
}
setParam('query','world','https://juejin.im/search?query=hello&time=2017-11-12')
// output: 
// "https://juejin.im/search?query=world&time=2017-11-12"複製代碼

移除單個參數

/** * [removeParam 移除單個參數] * @param {String} name * @param {String} url [default:location.href] * @return {String|Boolean} */
function removeParam(name, url) {
    if(typeof name !== 'string') return false;
    if (!url) url = window.location.href;
    var urlparts = url.split('?');
    var prefix = encodeURIComponent(name + '=');
    var pars = urlparts[1].split(/[&;]/g);
    var i = 0, len = pars.length;

    for (; i < len; i++) {
        if (encodeURIComponent(pars[i]).lastIndexOf(prefix, 0) !== -1) {
            pars.splice(i, 1);
        }
    }

    url = urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '');

    return url;
}
removeParam('query','https://juejin.im/search?query=hello&time=2017-11-12')
// output: 
// "https://juejin.im/search?time=2017-11-12"複製代碼

獲取多個參數

/** * [getParams 獲取多個參數] * @param {String} names [多個用空格分割] * @param {String} url [default:location.href] * @return {[String|Boolean]} */
function getParams(names, url) {
    if(typeof name !== 'string') return false;
    var names = names.split(' ');
    var result = {};
    var i = 0,
        len = names.length;
    if (names.length === 0) return false;
    for (; i < len; i++) {
        result[names[i]] = getParam(names[i], url);
    }
    return result;
}
getParams('query time','https://juejin.im/search?query=hello&time=2017-11-12')
// output: 
// {query: "hello", time: "2017-11-12"}複製代碼

設置多個參數

/** * [setParams 設置多個參數] * @param {Object} obj * @param {String} url [default:location.href] * @return {[String|Boolean]} */
function setParams(obj, url) {
    var result = url || '';
    if (Object.prototype.toString.call(obj) !== '[object Object]') return false;
    for (var name in obj) {
        result = setParam(name, obj[name], result);
    }
    return result;
}
setParams({a:111,b:222,query:'world'},'https://juejin.im/search?query=hello&time=2017-11-12')
// output: 
// "https://juejin.im/search?query=world&time=2017-11-12&a=111&b=222"複製代碼

移除多個參數

/** * [removeParams 移除多個參數] * @param {String} names [多個用空格分割] * @param {String} url [default:location.href] * @return {[String|Boolean]} */
function removeParams(names, url) {
    var result = url || '';
    var names = names.split(' ');
    var i = 0,
        len = names.length;
    if (names.length === 0) return false;

    for (; i < len; i++) {
        result = removeParam(names[i], result);
    }
    return result;
}
removeParams('query time','https://juejin.im/search?query=hello&time=2017-11-12')
// output: 
// "https://juejin.im/search"複製代碼

url hash 操做

/** * [getHash 方法] * @param {[String]} url [default:location.href] * @return {[String]} */
function getHash(url) {
    return decodeURIComponent(url ? url.substring(url.indexOf('#') + 1) : window.location.hash.substr(1));
}

/** * [setHash 方法] * @param {String} hash */
function setHash(hash) {
    window.location.replace('#' + encodeURIComponent(hash));
}

/** * [removeHash 方法] */
function removeHash() {
    window.location.replace('#', '');
}複製代碼

若是發現有BUG,請前往 GitHub 提 issuegit

若是以爲本文對您有幫助,請給個贊。(๑•ᴗ•๑)github

相關文章
相關標籤/搜索