Document.online event is fired on the <body> of each page when the browser switches between online and offline mode. Additionally, the events bubble up from document.body ,到 document , ending at window . Both events are non-cancellable (you can't prevent the user from coming online, or going offline).

window.navigator.onLine returns boolean true if the browser is online and false if it is definitely offline (disconnected from the network). The online and offline events are fired when the value of this attribute changes.

It is important to note that this event and attribute are inherently unreliable. A computer can be connected to a network without having Internet access.

You can register listeners for these events in a few familiar ways:

  • 使用 addEventListener window , document ,或 document.body
  • By setting the .ononline or .onoffline properties on document or document.body to a JavaScript 函数 object. ( 注意: 使用 window.ononline or window.onoffline will not work for compatibility reasons.)
  • By specifying ononline="..." or onoffline="..." attributes on the <body> tag in the HTML markup.

范例

There's a simple test case that you can run to verify that the events are working.

参考

元数据

  • 最后修改:
  1. DOM (文档对象模型)
  2. Document
  3. 构造函数
    1. Document()
  4. 特性
    1. alinkColor
    2. all
    3. anchors
    4. applets
    5. async
    6. bgColor
    7. body
    8. characterSet
    9. childElementCount
    10. children
    11. compatMode
    12. contentType
    13. currentScript
    14. defaultView
    15. designMode
    16. dir
    17. doctype
    18. documentElement
    19. documentURI
    20. documentURIObject
    21. domain
    22. domConfig
    23. 嵌入
    24. fgColor
    25. firstElementChild
    26. forms
    27. fullscreen
    28. fullscreenEnabled
    29. head
    30. height
    31. hidden
    32. 图像
    33. 实现
    34. lastElementChild
    35. lastModified
    36. lastStyleSheetSet
    37. linkColor
    38. 链接
    39. location
    40. mozSyntheticDocument
    41. onabort
    42. onafterscriptexecute
    43. onanimationcancel
    44. onanimationend
    45. onanimationiteration
    46. onauxclick
    47. onbeforescriptexecute
    48. onblur
    49. oncancel
    50. oncanplay
    51. oncanplaythrough
    52. onchange
    53. onclick
    54. onclose
    55. oncontextmenu
    56. oncuechange
    57. ondblclick
    58. ondurationchange
    59. onended
    60. onerror
    61. onfocus
    62. onfullscreenchange
    63. onfullscreenerror
    64. ongotpointercapture
    65. oninput
    66. oninvalid
    67. onkeydown
    68. onkeypress
    69. onkeyup
    70. onload
    71. onloadeddata
    72. onloadedmetadata
    73. onloadend
    74. onloadstart
    75. onlostpointercapture
    76. onmousedown
    77. onmouseenter
    78. onmouseleave
    79. onmousemove
    80. onmouseout
    81. onmouseover
    82. onmouseup
    83. onoffline
    84. ononline
    85. onpause
    86. onplay
    87. onpointercancel
    88. onpointerdown
    89. onpointerenter
    90. onpointerleave
    91. onpointermove
    92. onpointerout
    93. onpointerover
    94. onpointerup
    95. onreset
    96. onresize
    97. onscroll
    98. onselect
    99. onselectionchange
    100. onselectionchange
    101. onselectstart
    102. onsubmit
    103. ontouchcancel
    104. ontouchstart
    105. ontransitioncancel
    106. ontransitionend
    107. onvisibilitychange
    108. onwheel
    109. origin
    110. plugins
    111. policy
    112. popupNode
    113. preferredStyleSheetSet
    114. readyState
    115. referrer
    116. 脚本
    117. scrollingElement
    118. selectedStyleSheetSet
    119. styleSheetSets
    120. timeline
    121. title
    122. tooltipNode
    123. URL
    124. visibilityState
    125. vlinkColor
    126. width
    127. xmlEncoding
    128. xmlVersion
  5. 方法
    1. adoptNode()
    2. append()
    3. caretRangeFromPoint()
    4. clear()
    5. close()
    6. createAttribute()
    7. createCDATASection()
    8. createComment()
    9. createDocumentFragment()
    10. createElement()
    11. createElementNS()
    12. createEntityReference()
    13. createEvent()
    14. createExpression()
    15. createExpression()
    16. createNodeIterator()
    17. createNSResolver()
    18. createNSResolver()
    19. createProcessingInstruction()
    20. createRange()
    21. createTextNode()
    22. createTouch()
    23. createTouchList()
    24. createTreeWalker()
    25. enableStyleSheetsForSet()
    26. evaluate()
    27. evaluate()
    28. execCommand()
    29. exitFullscreen()
    30. exitPointerLock()
    31. getAnimations()
    32. getBoxObjectFor()
    33. getElementById()
    34. getElementsByClassName()
    35. getElementsByName()
    36. getElementsByTagName()
    37. getElementsByTagNameNS()
    38. hasFocus()
    39. hasStorageAccess()
    40. importNode()
    41. mozSetImageElement()
    42. open()
    43. prepend()
    44. queryCommandEnabled()
    45. queryCommandSupported()
    46. querySelector()
    47. querySelector()
    48. querySelectorAll()
    49. querySelectorAll()
    50. registerElement()
    51. releaseCapture()
    52. requestStorageAccess()
    53. write()
    54. writeln()
  6. 继承:
    1. 节点
    2. EventTarget
  7. DOM 相关页面
    1. AbortController
    2. AbortSignal
    3. Attr
    4. ByteString
    5. CDATASection
    6. CSSPrimitiveValue
    7. CSSValue
    8. CSSValueList
    9. CharacterData
    10. ChildNode
    11. 注释
    12. CustomEvent
    13. DOMConfiguration
    14. DOMError
    15. DOMErrorHandler
    16. DOMException
    17. DOMImplementation
    18. DOMImplementationList
    19. DOMImplementationRegistry
    20. DOMImplementationSource
    21. DOMLocator
    22. DOMObject
    23. DOMParser
    24. DOMPoint
    25. DOMPointInit
    26. DOMPointReadOnly
    27. DOMRect
    28. DOMString
    29. DOMTimeStamp
    30. DOMTokenList
    31. DOMUserData
    32. DocumentFragment
    33. DocumentType
    34. 元素
    35. ElementTraversal
    36. Entity
    37. EntityReference
    38. 事件
    39. EventTarget
    40. HTMLCollection
    41. MutationObserver
    42. 节点
    43. NodeFilter
    44. NodeIterator
    45. NodeList
    46. NonDocumentTypeChildNode
    47. ProcessingInstruction
    48. PromiseResolver
    49. 范围
    50. 文本
    51. TextDecoder
    52. TextEncoder
    53. TimeRanges
    54. TreeWalker
    55. TypeInfo
    56. USVString
    57. UserDataHandler
    58. XMLDocument