vue中禁止浏览器刷新和鼠标右键事件

发布时间 2023-06-25 14:23:26作者: 阿宇爱吃鱼

一、
created() {
    this.stopF5Refresh();
  },

stopF5Refresh() {
      document.onkeydown = function(e) {
        var evt = window.event || e;
        var code = evt.keyCode || evt.which;
        //屏蔽F1---F12
        if (code > 111 && code < 124) {
          if (evt.preventDefault) {
            evt.preventDefault();
          } else {
            evt.keyCode = 0;
            evt.returnValue = false;
          }
        }
      };
      //禁止鼠标右键菜单
      document.oncontextmenu = function(e) {
        return false;
      };
      //阻止后退的所有动作,包括 键盘、鼠标手势等产生的后退动作。
      history.pushState(null, null, window.location.href);
      window.addEventListener("popstate", function() {
        history.pushState(null, null, window.location.href);
      });
    },


二、

// 禁用F12

document.onkeydown = function(e) {

    var currKey = 0 

    vat evt = e || window.event

    currKey = evt.KeyCode || evt.which || evt.charCode

        // var e = window.event || arguments[0];

        if (e.keyCode == 123) {

            window.event.cancelBubble = true

            window.event.returnValue = false

            alert("F12被禁用");

            return false;

        }

    }

 

// 禁用右键

document.oncontextmenu =function(){

alert('右键被禁用');

returnfalse;

    }

 

// 禁止保存

document.onkeydown = function() {

        if ((e.ctrlKey) && (e.keyCode == 83)) { //ctrl+s

            alert("ctrl+s被禁用");

            return false;

        }

    }