onbeforeunload 事件在 JavaScript 中的用法是什麼?


如果您希望在文件載入之前觸發某個事件,那麼請使用 onbeforeunload 事件。

示例

可以嘗試執行以下程式碼來學習如何在 JavaScript 中實現 onbeforeunload 事件。

<!DOCTYPE html>
<html>
   <body onbeforeunload="return myFunction()">

      <a href="https://www.qries.com">Click to open Qries</a>
      <script>
         function myFunction() {
            return "Working with onbeforeunload event";
         }
      </script>
   </body>
</html>

更新日期:2020-05-21

124 次瀏覽

開啟您的職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.