The HTML ononline attribute is an event handler that triggers a script to execute when the browser's network connection status changes from offline to online.
Definition and Usage
➔ This is the opposite of the onoffline attribute.
➔ It is mainly used in the <body> element or Window object.
➔ Its primary purpose is to improve the user experience, by displaying a message about the network connection or need to adjust their functionality by going back online after the connection is restored.
Syntax
//In HTML
<body ononline="myFunction()"></body>
//In JavaScript
window.addEventListener("online", (event) => {
alert("You are now online!");
// Add code to handle online state
});
// Or using the event handler property:
window.ononline = (event) => {
alert("You are now online!");
};
function myFunction() {
alert("You are now online!");
}
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML ononline attribute example</title>
</head>
<body ononline="myFunction()">
<h1>HTML ononline attribute example</h1>
<p>Try enabling/disabling your network connection to trigger event.</p>
<script>
function myFunction() {
alert("You are now online!");
}
</script>
</body>
</html>