JS - BOM-window 常見應用-轉址、取網址、上下頁、重新整理、查詢連線狀態、列印

BOM (Browser Object Model) - window 常見應用。


window 物件

  1. history
  2. frame
  3. location
  4. DOM
  5. screen
  6. navigator

常見應用:

history

  • 上一頁

      window.history.back();
  • 下一頁

      window.history.forward();

location

  • 轉址

      window.location.href="http://google.com"
  • 重新整理頁面

      window.location.reload()
      window.location = location.href
  • 取當前網址-例:/posts/63534/#more

      window.location.href     // "/posts/63534/#more"
      window.location.origin   // "https://eudora.cc"
      window.location.protocol // "https:"
      window.location.hostname // "eudora.cc"
      window.location.pathname // "/posts/63534/"
      window.location.hash     // "#more"
  • 查詢是否為連線狀態

      window.navigator.onLine  // true = 連線中、false = 離線狀態

其他

  • 列印

      window.print()
  • 跳轉連結(預設為開啟新視窗,也還有其他參數可控制細節)

      window.open('http://google.com')

參考資料