Onload function is not working.I cannot play the song automatically when the song is loaded.Here is my view.
<div class="main" onload="enableAutoplay()">
<div class="audio-name">
<h1><?php echo str_replace('_',' ',$name);?></h1>
</div>
<center>
<audio id="audio" controls="controls" >
<source src="<?php echo base_url();?>musics/<?php echo $name;?>" type="audio/mp3" >
</audio>
</center>
<center><a href="<?php echo base_url();?>admin/audio/showaudio" class='back'>Back to All song list</a></center>
</div>
<script>
var vid = document.getElementById("audio");
function enableAutoplay() {
vid.autoplay = true;
vid.load();
}
function disableAutoplay() {
vid.autoplay = false;
vid.load();
}
function checkAutoplay() {
alert(vid.autoplay);
}
</script>
i like to play the song when user click the song name.but here user have to click the "play" button to play the song.
The onload
event can only be used on the body
itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload
event doesn't apply there.
as far as I know, onload
function can be used on <body>
tag, it doesn't seem to work on other tags. try adding it on <body>
or just running the function in js.