HTML onoffline Event Attribute

Description

The onoffline HTML event attribute triggers when the browser starts to work offline. This can happen for a variety of reasons, such as the user disconnecting from the internet or the network being unavailable.

The onoffline event attribute can be used to notify the user that the browser is offline and to take appropriate action. For example, a website could use the onoffline event attribute to display a message to the user informing them that they are offline and that certain features may not be available. The website could also use the onoffline event attribute to disable certain features, such as online forms or video streaming.

The onoffline event attribute is only supported by the <body> tag.

Here is an example of how to use the onoffline event attribute:

<body onoffline="alert('You are now offline.')">
  </body>

When the browser goes offline, an alert message will be displayed to the user.

It is important to note that the onoffline event attribute does not guarantee that the browser will be completely offline. It is possible that the browser may still be able to access certain resources, such as cached files or local storage. However, the onoffline event attribute can be used to take precautionary measures to ensure that the user has a good experience even when the browser is offline.

Syntax

<element onoffline="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE HTML>
<html>
<html>
<body ononline="onFunction()" onoffline="offFunction()">

<p>Open the File menu and click on the “Work offline” button to switch between working on onlie and offline.</p>

<p><strong>Note:</strong>Ononline and onoffline events are supported only in Firefox and Internet Explorer from versions 8 to 10.</p>

<script>
function onFunction() {
alert ("EXAMPLE: Your browser is online.");
}

function offFunction() {
alert ("EXAMPLE: Your browser is offline.");
}
</script>

</body>
</html>

Browser Support

The following table will show you the current browser support for the HTML onoffline Event Attribute.

Desktop
Edge Chrome Firefox Opera Safari
YesYesYesYesYes
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview
YesYesYesYesYesYes

Last updated by CSSPortal on: 14th October 2023