好用的js小组件

获取url的参数

2018-01-19  本文已影响0人  超爱吃小龙虾

let {location} = window;

const param = (() => {const args = {};let match = null; // 获取问好后面所有的字符串

const search = location.search.substring(1);// 这段正则一般用于提取URL中的参数,会把URL中?后的参数部分以&分割为参数对

const reg = /(?:([^&]+)=([^&]+))/g;

// exec() 方法用于检索字符串中的正则表达式的匹配。返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

 while ((match = reg.exec(search)) !== null) {

       if (match[2]) {

             // decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

             args[match[1]] = decodeURIComponent(match[2]);

 } }

 return args;

})();

param.debug = param.debug || window.debug;

export default param;

说明:保存该组件为param.js,ur例子http://222.222.222.222:8080/fed/index.html?cid='2344'&uid='yyu7'

组件调用

import param from 'param';

const data = { cid: param.cid, uid: param.uid};

上一篇 下一篇

猜你喜欢

热点阅读