码迷,mamicode.com
首页 > Web开发 > 详细

解析URL参数

时间:2018-05-03 12:49:56      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:参数   col   bst   解析   parse   example   window   ret   cti   

/**
 * 解析url参数
 * @example ?id=12345&a=b
 * @return Object {id:12345,a:b}
 */
export function urlParse () {
    let url = window.location.search;
    let obj = {};
    let reg = /[?&][^?&]+=[^?&]+/g;
    let arr = url.match(reg);
    // [‘?id=12345‘,‘&a:b‘]
    if (arr) {
        arr.forEach((item) => {
            let tempArr = item.substring(1).split(‘=‘);
            let key = decodeURIComponent(tempArr[0]);
            let val = decodeURIComponent(tempArr[1]);
            obj[key] = val;
        });
    }
    return obj;
}

 

解析URL参数

标签:参数   col   bst   解析   parse   example   window   ret   cti   

原文地址:https://www.cnblogs.com/lan1974/p/8984694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!