recherche

Maison  >  Questions et réponses  >  le corps du texte

javascript - 怎样用JS准确的判断浏览器刷新事件

因为需求,需要在页面中劫持刷新事件,找了下网上现有的方法都不好用,在关闭时也会起效。 谁有一个完美的刷新事件的判断方法。

大家讲道理大家讲道理2895 Il y a quelques jours470

répondre à tous(2)je répondrai

  • 高洛峰

    高洛峰2017-04-10 12:48:04

        <script language="javascript">   
            window.onbeforeunload = function() {   
                var n = window.event.screenX - window.screenLeft;   
                var b = n > document.documentElement.scrollWidth-20;   
                if(b && window.event.clientY < 0 || window.event.altKey){   
                    alert("这是一个关闭操作而非刷新");   
                    window.event.returnValue = ""; //此处放你想要操作的代码 
                }else{
                    alert("这是一个刷新操作而非关闭");   
                }
            }  
        </script>
    

    监听刷新就可以了

    répondre
    0
  • PHPz

    PHPz2017-04-10 12:48:04

    window.event.screenX 等值都为 undefined,只有 document.documentElement.scroll 有值。

    répondre
    0
  • Annulerrépondre