HTML - attributes - onl...

revision:


onload-related attributes

These attributes are part of the event attributes and can be used on the following elements:<audio>, <video>.

During the loading process of an audio/video, the following events occur, in this order: onloadstart, ondurationchange, onloadedmetadata, onloadeddata, onprogress, oncanplay, oncanplaythrough

"onload" attribute : when object has been loaded

This attribute fires when an object has been loaded.
"onload" is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.). However, it can be used on other elements as well.
The onload attribute is part of the event attributes and can be used on the following elements: <body>, <iframe>, <img>, <input>, <link>, <script>, <style>.

For input elements, the onload attribute is only supported when <input type="image">.

The "onload attribute" can be used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

syntax: <element onload="script"></element>

This attribute contains single value script and it runs when onload event is triggered.

some examples

example

Hello World!

codes:

                    <body onload="loadFunction()">
                        <h4>Hello World!</h4>
                    </body>
                    <script>
                        function loadFunction() {
                        alert("Page is loaded");
                        }
                    </script>
                

example

codes:

                    <img src="cartoon.jpg" onload="loadImage()" width="150" height="132">
                    <script>
                        function loadImage() {
                        alert("Image is loaded");
                        }
                    </script>
                

"onloadeddata" attribute occurs when data for the current frame is loaded, but not enough data to play next frame of the specified audio/video.

syntax: <element onloadeddata="script"></element>

This attribute contains single value script which works when onloadeddata event attribute is called.

some examples

example

example audio:

                    <audio style="margin-left:3vw;" id="myAudio" controls onloadeddata="playAudio()">
                        <source src="horse.wav" type="audio/wav">
                    Your browser does not support the audio element.
                    </audio>
                    <p class="spec">Play the audio file:</p>
                    <script>
                        function playAudio() {
                        alert("audiop data is loaded");
                        }
                    </script> 
                

example video

                    <video style="margin-left:3vw;" id="myVideo" width="320" height="176"
                    controls onloadeddata="playVideo()">
                        <source src="Wuzhen-20-10_02.mp4" type="video/mp4">
                        Your browser does not support HTML5 video.
                    </video>
                    <script>
                        function playVideo() {
                        alert("video data is loaded");
                        }
                    </script>   
                

"onloadedmetadata" attribute : when meta data for audio/video is loaded

The event occurs when meta data for the specified audio/video has been loaded. Meta data for audio/video consists of: duration, dimensions (video only) and text tracks.

syntax: <element onloadedmetadata="script"></element>

This attribute contains single value script which works when onloadedmetadata event attribute is called.

some examples

example

example audio

                    <audio style="margin-left:3vw;" id="myAudio" controls onloadedmeta data="playAudio()">
                        <source src="horse.wav" type="audio/wav">
                    Your browser does not support the audio element.
                    </audio>
                    <p class="spec">Play the audio file:</p>
                    <script>
                        function playAudio() {
                        alert("audio meta data is loaded");
                        }
                    </script> 
                

example video

                    <video style="margin-left:3vw;" id="myVideo" width="320" height="176"
                    controls onloadeddata="playVideo()">
                        <source src="Wuzhen-20-10_02.mp4" type="video/mp4">
                        Your browser does not support HTML5 video.
                    </video>
                    <script>
                        function playVideo() {
                        alert("video meta data is loaded");
                        }
                    </script>   
                

"onloadstart" attribute fires when the browser starts looking for the specified audio/video.

This is when the loading process starts.

syntax: <element onloadstart="script"></element>

This attribute contains single value script which works when onloadstart event attribute is called.

some examples

example

example audio

                    <audio style="margin-left:3vw;" id="myAudio" controls onloadstart="myAudio()">
                    <source src="horse.wav" type="audio/wav">
                    Your browser does not support the audio element.
                    </audio>
                    <p class="spec">Play the audio file</p>
                    <script>
                        function myAudio() {
                            alert("The audio file has started loading");
                        }
                    </script> 
                

example video:

                    <video style="margin-left:3vw;" id="myVideo" width="320" height="176" controls
                    onloadstart="myVideo()">
                        <source src="Wuzhen-20-10_02.mp4" type="video/mp4">
                        Your browser does not support HTML5 video.
                    </video>
                    <p class="spec">Play the video:</p>
                    <script>
                        function myVideo() {
                        alert("The video file has started loading");
                        }
                    </script>