禁止用戶打開控制檯調試代碼

1,禁用F12,阻止默認行爲調試

window.onkeydown=window.onkeyup=window.onkeypress=function(event){io

if(event.keyCode==123){event

    event.prevnentDefault()function

    window.event.returnValue=false;快捷鍵

  }co

return

 

2,禁用右鍵context

window.oncontextmenu=function(event){location

  event.preventDefault();

  return false;

}

3,禁用預先調試

除了打開F12,右鍵,用戶還有可能先打開任意網頁的F12,而後在地址欄輸入網址,或者使用快捷鍵ctrl+shift+i打開控制檯

因此對於這兩種狀況,咱們能夠經過  比較屏幕window.outerWidth和頁面可見區域window.innerWidth的差距判斷是否打開控制檯;

var threshold=160;

setInterval(function(){

if(window.outerWidth-window.innerWidth>threshold || window.outerHeight-window.innerHeight>threshold){

  //打開控制檯就刷新頁面;

    window.location.reload()

  }

})  

相關文章
相關標籤/搜索