Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onsuspend — handling suspended media fetching

The onsuspend attribute specifies JavaScript to run for the suspend event. For audio and video, this event means the browser is intentionally not fetching data at present, not that playback has been paused.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onsuspend="JavaScript statements"

onsuspend is a global event handler content attribute for the suspend event. Its value is JavaScript code, not a resource address or a Boolean value. Although the attribute is available on HTML elements, its usual application is to audio and video elements.

The browser fires suspend when it intentionally suspends fetching media data, setting networkState to NETWORK_IDLE. For example, it may wait for playback to start before downloading more data. This does not itself mean an error or paused playback: pause concerns playback, whereas stalled concerns the browser trying to fetch data but not receiving it.

Inside the attribute code, event provides the event object. In JavaScript, you can instead assign a function to the element.onsuspend property or use element.addEventListener('suspend', handler). The event name does not include the on prefix.

The example dispatches a synthetic event to make the result independent of networking and browser buffering decisions. It tests only execution of the attribute code; it neither suspends fetching nor changes networkState. A page's security policy can block event handler code written directly in attributes.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onsuspend demo</title>
<audio id="media" onsuspend="document.getElementById('result').textContent = event.type"></audio>
<output id="result">pending</output>
<script>
  document.getElementById('media').dispatchEvent(new Event('suspend'));
</script>
</html>

Result

suspend

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources