全为前端代码,把以下代码片段加入js中即可 <script></script>,或引入
方法一:
可以有效禁止F12和右击(单击浏览器设置里 开发者-审查元素 无法禁止)
- <script>
- //屏蔽F12和右键
- function click(e) {
- if (document.all) {
- if (event.button == 2 || event.button == 3) {
- alert("欢迎光临寒舍!!");
- oncontextmenu = 'return false';
- }
- }
- if (document.layers) {
- if (e.which == 3) {
- oncontextmenu = 'return false';
- }
- }
- }
- if (document.layers) {
- document.captureEvents(Event.MOUSEDOWN);
- }
- document.onmousedown = click;
- document.oncontextmenu = new Function("return false;")
- document.onkeydown = document.onkeyup = document.onkeypress = function () {
- if (window.event.keyCode == 123) {
- window.event.returnValue = false;
- return (false);
- }
- }
- </script>
方法二:
可以有效禁止F12和右击、审查元素----设置中强行打开开发者会直接关闭网页。
- <script>
- //禁用右键(防止右键查看源代码)
- window.oncontextmenu = function () { return false; }
- //禁止任何键盘敲击事件(防止F12和shift+ctrl+i调起开发者工具)
- window.onkeydown = window.onkeyup = window.onkeypress = function () {
- window.event.returnValue = false;
- return false;
- }
- //如果用户在工具栏调起开发者工具,那么判断浏览器的可视高度和可视宽度是否有改变,如有改变则关闭本页面
- var h = window.innerHeight, w = window.innerWidth;
- window.onresize = function () {
- if (h != window.innerHeight || w != window.innerWidth) {
- window.close();
- window.location = "about:blank";
- }
- }
- </script>
在demo阶段发现两个问题:
方法会导致手机端无法滑动,滑动或者操作会被判定为改变分辨率造成强制关闭网页的情况。
在别的页面打开F12在加载自己的网页时,虽然也及时关闭,但在响应里还是可以看到流量包。
方法三:
该方法有效禁止F12,强行打开会重定向到自己定义的页面去,可以直接引入js,或者你下载放自己服务器上
- <script src="https://www.58soho.cn/js/console-ban.min.js"></script>
- //引用方法
- <script>
- // default options
- ConsoleBan.init()
- // custom options
- ConsoleBan.init({
- redirect: 'https://www.58soho.cn/404.html'
- })
- </script>