HTML onsuspend Event Attribute

If this site has been useful, we’d love your support! Running this site takes time and resources, and every small contribution helps us keep creating valuable content. Consider buying us a coffee to keep things going strong!

Description

The HTML onsuspend event attribute specifies a JavaScript function to be executed when the browser is intentionally not getting media data. This event occurs when the loading of the media is suspended (prevented from continuing). This can happen when the download has completed, or because it has been paused for some reason.

The onsuspend event attribute can be used on the following HTML elements:

  • <audio>
  • <video>

To use the onsuspend event attribute, simply add it to the HTML element you want to listen for the event on, and specify the name of the JavaScript function to be executed when the event occurs. For example, the following code would execute the mySuspendFunction() function when the <audio> element's loading is suspended:

<audio onsuspend="mySuspendFunction()">
  <source src="my-audio-file.mp3" type="audio/mpeg">
</audio>

The mySuspendFunction() function could then be used to perform any actions you need, such as displaying a message to the user, or pausing the playback of the media.

Here is an example of a complete HTML document that uses the onsuspend event attribute:

<!DOCTYPE html>
<html>
<head>
  <title>HTML onsuspend Event Example</title>
</head>
<body>

  <audio onsuspend="mySuspendFunction()">
    <source src="my-audio-file.mp3" type="audio/mpeg">
  </audio>

  <script>
    function mySuspendFunction() {
      // Display a message to the user
      alert("The audio has been suspended.");
    }
  </script>

</body>
</html>

When the user loads this document, the <audio> element will begin downloading the audio file. If the download is suspended for any reason, the mySuspendFunction() function will be executed. This function will display a message to the user informing them that the audio has been suspended.

Syntax

<element onsuspend="script">

Values

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

Example

<!DOCTYPE html>
<html>
<body>
<video controls onsuspend="myFunction()">
<source src="sample.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>

<script>
function myFunction() {
alert("EXAMPLE: Media loading suspended");
}
</script>

</body>
</html>

Browser Support

The following table will show you the current browser support for the HTML onsuspend 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