js去除url指定参数

/**
* @description Remove the specified params
* @param {array} params
* @return {string} res
*/
const handleRemoveParam = (params = []) => {
const { origin, pathname, search } = window.location;
const arr = search.substring(1).split("&");
let collect = [];

arr.forEach((item, ind) => {
params.forEach((param) => {
if (item.includes(`${param}=`)) {
collect = [...collect, ind];
}
});
});

for(let i = 0; i < collect.length; i++) {
const item = i > 0 ? collect[i] - i : collect[i];
arr.splice(item, 1);
}

const newSearch = arr.join('&');
const res = newSearch ? origin + pathname + '?' + newSearch : origin + pathname;
return res;
}

const str = handleRemoveParam(['bbb', 'aaa', 'token']);
console.log(str);

js数组去重的方法

Array.prototype.unique = function () {
var r = []
var n = {}

for(var i = 0; i < this.length; i++) {
var val = this[i]
var type = typeof val

if (!n[val]) {
r.push(val)
n[val] = [type]
} else if (n[val].indexOf(type) < 0) {
r.push(val)
n[val].push(type)
}
}

return r
}
var arr = [112,112,34,'你好','112',112,34,'你好','str','str1', {}, {}, null, null]

arr.unique() // [112, 34, "你好", "112", "str", "str1", {…}, null]

js focus end

export let setFocus = (target) => {
let t = jQuery(target).val()
jQuery(target).val('').focus().val(t)
}
setFocus('input')

js链式取值(函数解析字符串)

开发中,链式取值是非常正常的操作,如:
res.data.goods.list[0].price
有时候会出现错误:
Uncaught TypeError: Cannot read property 'goods' of undefined

通过函数解析字符串验证

function get(obj, props, def) {
if ((obj == null) || obj == null || typeof props !== 'string') return def;

const temp = props.split('.');
const fieldArr = [].concat(temp);

temp.forEach((e, i) => {
if (/^(\w+)\[(\w+)\]$/.test(e)) {
const matchs = e.match(/^(\w+)\[(\w+)\]$/);
const field1 = matchs[1];
const field2 = matchs[2];
const index = fieldArr.indexOf(e);
fieldArr.splice(index, 1, field1, field2);
}
})

return fieldArr.reduce((pre, cur) => {
const target = pre[cur] || def;

if (target instanceof Array) {
return [].concat(target);
}
if (target instanceof Object) {
return Object.assign({}, target)
}
return target;
}, obj)
}

使用:

var c = {
  a: {
    b: [1, 2, 3]
  }
}
get(c, 'a.b') // [1,2,3]
get(c, 'a.b[1]') // 2
get(c, 'a.d', 12) // 12

js获取url参数方法

export let getUrlParam = (name) => {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
let r = window.location.search.substr(1).match(reg)

if (r != null) {
return decodeURI(r[2])
} else {
return null
}
}

点击倒计时countdown

// countdown
var $timeBox = $(".btn-verify"),
setTime = 5, //set second
paramTime,
Message = {
second: '秒后获取',
verify: '获取验证码'
};

function funTimeout() {
paramTime--;

$timeBox.html(paramTime + Message.second); //show

if (paramTime == 0) {
$timeBox.addClass('able').html(Message.verify);
} else {
setTimeout("funTimeout()", 1000);
}

}

$(function () {

$timeBox.on('click', function () {
var $this = $(this);
if ($this.hasClass('able')) {
$this.removeClass('able');
paramTime = setTime;
funTimeout();
}
})

})
<div class="btn-verify able">Click</div>

当弹出popup悬浮层时,禁止body滚动

示例:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<title>test</title>
<style>
p { line-height: 110px; }
.btn { position: fixed; top: 10px; right: 10px; z-index: 9; padding: 5px 10px; color: #fff; background: #c00; }
.mask { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0,0,0,.5); z-index: 99; display: none; }
.popup { position: fixed; top: 0; right: 0; bottom: 0; left: 40px; background: #fff; z-index: 101; overflow-y: auto; display: none; }
</style>
</head>
<body>
<div class="btn">Click</div>
<div class="main"><p>111</p><p>222</p><p>333</p><p>444</p><p>555</p><p>666</p><p>777</p><p>888</p><p>999</p></div>
<div class="mask"></div>
<div class="popup"><p>111</p><p>222</p><p>333</p><p>444</p><p>555</p><p>666</p><p>777</p><p>888</p><p>999</p></div>
</body>
<script src="jquery-1.10.1.min.js"></script>
</html>
<script>
var $body = $('body'),
scrollTop;

$('.btn').on('click', function() {
$('.mask, .popup').show();
scrollTop = $body.scrollTop(); //body设置为fixed之后会飘到顶部,所以要动态计算当前用户所在高度
$body.css({
'overflow':'hidden',
'position': 'fixed',
'top': scrollTop*-1
});
});

$('.mask').on('click', function() {
$('.mask, .popup').hide();
/*取消后设置回来*/
$body.css({
'overflow':'auto',
'position': 'static',
'top': 'auto'
}).animate({ scrollTop: scrollTop }, 0);
});

</script>