url解析函数:
效果图:
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(//([^/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^/])/,'/$1'),
relative: (a.href.match(/tps?://[^/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^//,'').split('/')
};
}
·
多维数组根据某个字段排序(默认升序,)
var obj = [{"sort":1,"data":"xx你好"},{"sort":5,"data":"meet是一种缘分"}];
obj.sort(function (obj1, obj2) {
var val1 = obj1.sort;
var val2 = obj2.sort;
if (val1 < val2) { //如需降序 改成 '>'
return - 1;
} else if (val1 > val2) { //如需降序 改成 '>'
return 1;
} else {
return 0;
}
});
·
字符串转utf-8
function toUtf8(str) {
var out, i, len, c;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
.
复制指定内容
function copy(text, callback){
var tag = document.createElement('input');
tag.setAttribute('id', 'cp_hgz_input');
tag.value = text;
document.getElementsByTagName('body')[0].appendChild(tag);
document.getElementById('cp_hgz_input').select();
document.execCommand('copy');
document.getElementById('cp_hgz_input').remove();
if(callback) {callback(text)}
}
字符串去重
/**
* 过滤相同字符串:
* 111
* 222
* 111
* 333
* @param s
* @param mark
* @returns {string}
*/
filterEquallyMember: function filterEquallyMember(s, mark) {
var arr = s.split(mark);
var x = new Set(arr);
var distinctResult = "";
x.forEach(function (element, sameElement, x) {
distinctResult += element + ",";
});
if (distinctResult) {
distinctResult = distinctResult.substring(0, distinctResult.length - 1);
}
return distinctResult;
}