首页 > 文章列表 > 当HTML文档即将被卸载时执行脚本?

当HTML文档即将被卸载时执行脚本?

卸载 脚本执行 HTML文档
412 2023-09-18

当文档准备卸载时,onbeforeunload 事件属性会触发。

示例

您可以尝试运行以下代码来实现 onbeforeunload 属性 −

<!DOCTYPE html>
<html>
   <body onbeforeunload = "return display()">
      <p>Close this window or press F5.</p>
      <script>
         function display() {
            return "Wanna stay here or leave?";
         }
      </script>
   </body>
</html>