HTML DOM PageTransition 事件


DOM PageTransitionEvent 是當用戶在網頁間導航時發生的事件。

PageTransitionEvent 物件的屬性

屬性
說明
persisted
它根據網頁是否已快取返回 true 或 false。

屬於 PageTransitionEvent 物件的事件型別

事件
說明
pageHide
當用戶導航離開一個網頁時發生。
pageShow
當用戶導航到一個網頁時發生。

範例

讓我們看一個 PageTransitionEvent 物件的範例 -

 實際範例

<!DOCTYPE html>
<html>
<head>
<style>
   body{
      text-align:center;
      color:#fff;
      background: #ff7f5094;
      height:100%;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM PageTransitionEvent Event Demo</h1>
<button onclick="checkPage(event)" class="btn">Click me</button>
<script>
   function checkPage(event) {
      if (event.persisted) {
         confirm("Page was cached by the browser");
      } else {
         confirm("Page was not cached by the browser");
      }
   }
</script>
</body>
</html>

輸出

它將產生以下輸出 -

點選“單擊我”按鈕以瞭解瀏覽器是否快取了當前頁面。

更新於: 2019 年 7 月 30 日

108 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始
廣告