Audio preload Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

The preload property sets or gets the preload attribute of a audio, which controls if the audio should be loaded when the page loads.

The preload attribute is ignored if the autoplay attribute is present.

Set the preload property with the following Values

ValueDescription
auto browser should load the entire audio when the page loads
metadata browser should load only metadata when the page loads
none browser should NOT load the audio when the page loads

Return Value

A String, representing what data should be preloaded (if any).

Possible return values are "auto", "metadata", or "none".

The following code shows how to check if and how the author thinks that the audio should be loaded when the page loads:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction('none')">Audio without preload</button>
<button onclick="myFunction('auto')">Audio with preload</button>
<br>

<script>
function myFunction(p) {
    var x = document.createElement("AUDIO");
    x.setAttribute("controls", "controls");

    var y = document.createElement("SOURCE");
    y.setAttribute("src", "your.ogg");
    y.setAttribute("type", "audio/ogg");

    x.appendChild(y);/*ww  w .jav  a 2s.  c o  m*/
    var z = document.createElement("SOURCE");
    z.setAttribute("src", "your.mp3");
    z.setAttribute("type", "audio/mpeg");
    x.appendChild(z);

    // Set the preload property:
    x.preload = p;

    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials