john oliver john oliver - 2 years ago 108
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>


<audio id="audio" controls="controls" >
<source src="<?php echo base_url();?>musics/<?php echo $name;?>" type="audio/mp3" >

<center><a href="<?php echo base_url();?>admin/audio/showaudio" class='back'>Back to All song list</a></center>


var vid = document.getElementById("audio");
function enableAutoplay() {
vid.autoplay = true;

function disableAutoplay() {
vid.autoplay = false;

function checkAutoplay() {

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download