
js常用代码
判断是否手机端
if (/Android|webOS|iPhone|iPad|BlackBerry/i.test(navigator.userAgent)) {}
复制功能
var text = '我是复制的内容';
var oInput = document.createElement('input');
oInput.value = text;
document.body.appendChild(oInput);
oInput.select();
document.execCommand("Copy");
oInput.className = 'oInput';
oInput.style.display = 'none';
禁止按f12调试
document.onkeydown = function () {
if (window.event && window.event.keyCode == 123) {
event.keyCode = 0;
event.returnValue = false;
// alert("谢谢合作!");
// location.href = ''
}
if (window.event && window.event.keyCode == 13) {
window.event.keyCode = 505;
}
if (window.event && window.event.keyCode == 8) {
alert(str + "\n请使用Del键进行字符的删除操作!");
window.event.returnValue = false;
}
}
禁止页面右键
document.oncontextmenu = function () {
// alert("谢谢合作!");
event.returnValue = false;
location.href = ''
}
禁止 ctrl + u 查看源码
document.addEventListener('keydown', function (event) {
if (event.ctrlKey && event.key === 'u') {
event.preventDefault();
// alert("谢谢合作!");
location.href = ''
}
});
打开控制台 无限debug
(() => {
function ban() {
setInterval(() => {
Function('debugger')()
}, 50);
}
try {
ban()
} catch (err) { }
})()
判断pc浏览器是否缩放,若返回100则为默认无缩放,如果大于100则是放大,否则缩小
function detectZoom() {
var ratio = 0,
screen = window.screen,
ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
} else if (~ua.indexOf('msie')) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
} else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
ratio = window.outerWidth / window.innerWidth;
}
if (ratio) {
ratio = Math.round(ratio * 100);
}
return ratio;
};
alert(detectZoom())
判断PC端浏览器缩放比例不是100%时的情况,配合 detectZoom()
使用
function isScale() {
var rate = detectZoom();
if (rate != 100) {
//如何让页面的缩放比例自动为100,'transform':'scale(1,1)'没有用,又无法自动条用键盘事件,目前只能提示让用户如果想使用100%的比例手动去触发按ctrl+0
console.log(1)
alert('当前页面不是100%显示,请按键盘ctrl+0恢复100%显示标准,以防页面显示错乱!');
}
}
$(window).on('resize', function() {
isScale();
});
阻止pc端浏览器缩放js代码
由于浏览器菜单栏属于系统软件权限,没法控制,我们着手解决ctrl/cammond + +/- 或 Windows下ctrl + 滚轮 缩放页面的情况,只能通过js来控制了
jqeury version
$(document).ready(function () {
// chrome 浏览器直接加上下面这个样式就行了,但是ff不识别
$('body').css('zoom', 'reset');
$(document).keydown(function (event) {
//event.metaKey mac的command键
if ((event.ctrlKey === true || event.metaKey === true) && (event.which === 61 ||
event.which === 107 ||
event.which === 173 || event.which === 109 || event.which === 187 || event.which ===
189)) {
event.preventDefault();
}
});
$(window).bind('mousewheel DOMMouseScroll', function (event) {
if (event.ctrlKey === true || event.metaKey) {
event.preventDefault();
}
});
});
判断浏览器类型以及ie版本
唯一缺点就是 IE7与IE5不分,但是IE6以下的判别已经不重要了
function BrowserType() {
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
// var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
var isIE = window.ActiveXObject || "ActiveXObject" in window
// var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; //判断是否IE的Edge浏览器
var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1 && !isEdge; //判断Chrome浏览器
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (userAgent.indexOf('MSIE 6.0') != -1) {
return "6";
} else if (fIEVersion == 7) {
return "7";
} else if (fIEVersion == 8) {
return "8";
} else if (fIEVersion == 9) {
return "9";
} else if (fIEVersion == 10) {
return "10";
} else if (userAgent.toLowerCase().match(/rv:([\d.]+)\) like gecko/)) {
return "11";
} else {
return "0"
} //IE版本过低
} //isIE end
if (isFF) {
return "21";
}
if (isOpera) {
return "22";
}
if (isSafari) {
return "23";
}
if (isChrome) {
return "24";
}
if (isEdge) {
return "25";
}
}
var browser = BrowserType()
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员小嵩
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果