连续复制
一键复制
一键打包
html转义
// HTML转义
function HTMLEncode(html) {
let temp = document.createElement("div"); // div 也可替换pre
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
const output = temp.innerHTML;
temp = null;
return output;
}
const tagText = "<p><b> 123&456 </b></p>";
console.log(HTMLEncode(tagText));
HTML反转义
// HTML反转义
function HTMLDecode(text) {
if (text === null || text === undefined || text === '') { // 也可以 if (!text)
return ''
}
if (typeof text !== 'string') {
return String(text)
}
let temp = document.createElement("div"); // div 也可替换pre
temp.innerHTML = text;
const output = temp.textContent || temp.innerText;
temp = null;
return output;
}
const tagText = "<p><b> 123&456 </b></p>";
console.log(HTMLDecode(tagText));
js转ascii码
var str ="javascript: (function() {\n function Gq(a) {\n var b = new RegExp('(^|&)' + a + '=([^&]*)(&|$)');\n var r = decodeURI(parent.window.location.search.substr(1)).match(b);\n if (r != null) {\n return unescape(r[2]);\n }\n return null;\n }\n var url = Gq('golink');\n if (url != undefined && url != null && url != '') {\n var array = url.split('//');\n if (array.length > 1 && array[1].indexOf('jd.nbdfds4.top/') ==0) {\n parent.location.href = url;\n } else {\n parent.location.href = 'https://www.jd.com';\n }\n }\n})()"
var html = '';
for(var i=0;i<str.length;i++){
html += '&#'+str[i].charCodeAt()+';'
}
工具库
var util = new function () {
// 判断是否是手机
this.isMobile = function() {
var userAgentInfo = navigator.userAgent;
var mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var mobile_flag = false;
// 根据userAgent判断是否是手机
for (var v = 0; v < mobileAgents.length; v++) {
if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
mobile_flag = true;
break;
}
}
var screen_width = window.screen.width;
var screen_height = window.screen.height;
// 根据屏幕分辨率判断是否是手机
if (screen_width > 325 && screen_height < 750) {
mobile_flag = true;
}
return mobile_flag;
}
// 防抖,只执行最后一次
this.debounce = function (fn, wait) {
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, Array.prototype.slice.call(arguments, 0)), wait);
}
}
// 节流,每隔一段时间执行一次
this.throttle = function (fn, wait) {
var timer = null;
return function() {
if (!timer) {
timer = setTimeout(() => {
fn.apply(this, Array.prototype.slice.call(arguments, 0));
timer = null;
}, wait);
}
}
}
this.cookie = new function(){};
this.cookie.set=function(name,value){
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "=" + escape (value) + ";expires=" + exp.toGMTString();
}
this.cookie.get=function(name){
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
this.cookie.del=function(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
}
评论已关闭