2013-06-25 232 views
0

我试图设置一个客户端的网站来播放页面加载(在他的需求,而不是我的首选功能)的音频文件。我已经使用了一些简单的JavaScript,但音频在页面加载时不播放。任何想法为什么发生这种情况?音频不播放 - JavaScript

<head> 
<!--other metadata up here--> 
    <script> 
function EvalSound(soundobj) { 
     var thissound = document.getElementById(soundobj); 
     thissound.Play(); 
    } 
    </script> 

    <embed src="scripts/audio.wav" autostart=true width=1 height=1 id="audio" 
    enablejavascript="true"> 
</head> 
<body> 
    <div id="container" onload="EvalSound('audio')"> 
<!--rest of page> 
</body> 

回答

1

HTML5溶液,(IE9 +,和所有其他适当的浏览器都支持)

<audio src="audio.mp3" preload="auto" controls></audio> 

<script> 
    var getAudio = document.getElementsByTagName("audio")[0]; 
    getAudio.play(); 
</script> 

自动玩没有办法阻止它的音频虽然是一个可怕的想法。

+0

什么是放在HTML内的最好的地方? – mikedugan

+0

把'audio'标签放在'body'的某个地方,然后'Javascript'放在底部,靠近'body'标签。 –

0

Divs没有加载事件。你应该把它附着在身体上。

+0

我试过这样做,并添加一个空的表单使用onload事件为此无济于事。 – mikedugan