john oliver john oliver - 4 months ago 9
PHP Question

Onload function not working?

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.

Answer

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.