zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    
  • 相关阅读:
    植物大战僵尸英文原版
    2020-5-13递归练习 六人参加竞赛
    2020-5-1递归练习
    java当脚本用。QQ表白轰炸机
    面向实战---VUE项目的文件加载顺序,或者加载流程,以及index.html,main.js,app.vue的区别
    CSS多个view随机分布,不重叠,如何实现呢?
    vite项目才踩坑日志1
    css属性之clip-path
    纯CSS3实现的阳光海鸥沙滩遮阳伞和比基尼美女风景动画效果源码
    TP5 中使用wherein 进行查询,太慢了,怎么优化?
  • 原文地址:https://www.cnblogs.com/sntetwt/p/3337451.html
Copyright © 2011-2022 走看看