在 HTML 中,当文档即将卸载时执行脚本?

htmlweb developmentfront end technology更新于 2025/8/17 23:08:17

当文档准备好卸载时,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>

相关文章


有用资源