Home  >  Q&A  >  body text

javascript - How to monitor browser refresh, close, and back behavior on PC?

How to monitor the browser refresh, close, and back behavior on PC? Then pop up the custom layer?
I tried onbeforeunload method. No alert pops up.

为情所困为情所困2663 days ago1043

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-07-05 10:58:28

    unload cannot preventDefault, the reason is obvious when you think about it, to prevent users from being unable to close the webpage

    This is what you should do:

    onbeforeunload = function (){
      if (unsaved) return '本次编辑未保存, 确认关闭网页?'
    }

    reply
    0
  • Cancelreply