zoukankan      html  css  js  c++  java
  • jQuery 解析 url 参数

    应用场景:

    三毛:我现在拿到一个 url 地址(https://www.google.com/search?dcr=0&ei=5C&q=param),我现在要获取 location.search 后的参数,并组成一个对象,{dcr: '0', ej: '5C', q: 'param'},怎么处理?
    
    五毛:呃,稍等,我去谷歌一下

    谷歌结果:

    // 解析 url 参数
    (function($) {
        var re = /([^&=]+)=?([^&]*)/g,
            decodeRE = /+/g,
            decode = function (str) { return decodeURIComponent( str.replace(decodeRE, " ") ); };
    
        $.parseParams = function(query) {
            let params = {}, e;
    
            while ( e = re.exec(query) ) params[ decode(e[1]) ] = decode( e[2] );
            return params;
        };
    })(jQuery);

    如何使用:

    $.parseParams(location.href.split('?')[1] || '');

    举个栗子:

    var url = 'https://www.google.com/search?dcr=0&ei=5C&q=param', // 模拟的 url 地址
        param = $.parseParams(url.split('?')[1] || ''); // 解析问号后的 url 参数
    console.log(param); // {dcr: "0", ei: "5C", q: "param"}
  • 相关阅读:
    线段树模板
    树状数组练习
    树状数组模板
    codeforce——思维dp
    fib博弈
    寒假总结
    相邻的数互质
    大数取模运算
    阶乘因式分解(一)
    1和0既非素数也非合数
  • 原文地址:https://www.cnblogs.com/lpbottle/p/8117371.html
Copyright © 2011-2022 走看看