• javascript获取和设置URL中的参数


    勘误版

    function getQuery(key, url) {
        url = url || window.location.href;
        if (url.indexOf('#') !== -1)
            url = url.substring(0, url.indexOf('#'));
        var rts = [], rt;
        var queryReg = new RegExp('(^|\?|&)' + key + '=([^&]*)(?=&|$|#)', 'g');
        while ((rt = queryReg.exec(url)) != null) {
            rts.push(decodeURIComponent(rt[2]));
        }
        if (rts.length == 0) return null;
        if (rts.length == 1) return rts[0];
        return rts;
    }
    
    
    function setQuery(key, value, url) {
    
        url = url || window.location.href;
        var hash = '';
    
        if (url.indexOf('#') !== -1)
            hash = url.substring(url.indexOf('#'));
    
        url = url.replace(hash, '');
        url = url.replace(new RegExp('(^|\?|&)' + key + '=[^&]*(?=&|#|$)', 'g'), '');
        value = isArray(value) ? value : [value];
    
        for (var i = value.length - 1; i >= 0; i--) {
            value[i] = encodeURIComponent(value[i]);
        }
    
        var p = key + '=' + value.join('&' + key + '=');
        return url + (/?/.test(url) ? '&' : '?') + p + hash;
    }
    

     增强版,支持同名参数

    function getQuery(key, url) {
        url = url || window.location.href;
        var rts = [], rt;
        var queryReg = new RegExp('(^|\?|&)' + key + '=([^&]*)(?=&|$|#)', 'g');
        while ((rt = queryReg.exec(url)) != null) {
            rts.push(decodeURIComponent(rt[2]));
        }
        if (rts.length == 0) return null;
        if (rts.length == 1) return rts[0];
        return rts;
    }
    
    
    function setQuery(key, value, url) {
    
        url = url || window.location.href;
        url = url.replace(new RegExp('(^|\?|&)' + key + '=[^&]*(?=&|$|#)', 'g'), '');
        value = value.splice ? value : [value];
    
        for (var i = value.length - 1; i >= 0; i--) {
            value[i] = encodeURIComponent(value[i]);
        }
    
        var p = key + '=' + value.join('&' + key + '=');
        return url + (/?/.test(url) ? '&' : '?') + p;
    }
    

     利用正则表达式

    function getQuery(key, url) {
        var reg = new RegExp('^\S*(\?|&)' + key + '=([^&]*)\S*$');
        var l = url || window.location.href;
        if (reg.test(l)) {
            return decodeURIComponent(l.replace(reg, '$2'));
        } else {
            return null;
        }
    }
    
    function setQuery(key, value, url) {
        var reg = new RegExp(key + '=[^&]*(?=&|$)');
        var l = url || window.location.href;
        if (reg.test(l)) {
            return l.replace(reg, key + '=' + encodeURIComponent(value));
        } else {
            return l + (/?/.test(l) ? '&' : '?') + key + '=' + encodeURIComponent(value);
        }
    }
    
  • 相关阅读:
    当初为蜂巢样式实验过的方法
    在看 jquery 源码中发现的一些优化方向
    我终于有案例库啦(github 提供的)
    学习笔记(五)
    试坑不完美的 clip-path (我说的 CSS 的那个)
    解决安卓机在微信上播放视频有广告问题
    requestAnimationFrame 的实验性实践
    学习笔记(四)
    ajax
    php 增删改查---增
  • 原文地址:https://www.cnblogs.com/sntetwt/p/3337451.html
Copyright © 2020-2023  润新知