Giving background music to a webpage
var musicsrc="jyothir2.mp3";
if (navigator.appName=="Microsoft Internet Explorer")
document.write('<bgsound src='+'"'+musicsrc+'"'+' loop="infinite">')
else
document.write('<embed src=\"'+musicsrc+'\" hidden="true" border="0" width="20" height="20" autostart="true" loop="infinite">')
friends my javascript( for开发者_StackOverflow社区 background music) codes is producing a continuous music in IE but the music is not continuous in other browsers.. can anybody tell me why is it so?
Worth It?
It is well worth considering whether playing continuous background music to all your visitors is something you actually want to do. Many people feel it is a bit of an invasion for sites to just decide what they will hear. This is particularly a problem for people who may be using their computers to listen to their own choice of music. It also adds significantly to the bandwidth used by your site. Be sure this is something visitors will find of value before going ahead with it.
The Solution
If you decide this is something you and your visitors want and will appreciate, the solution is simple. For the non-IE (embed
element) version, you should set the loop attribute to either true
or false
. Setting it to true
will cause it to loop indefinitely, and setting it to false
will not. It will not know what to do with the value of infinite
, so it is most likely interpreting it as the default, false
.
In addition, I have also noticed you are escaping your double-quotes with a backslash. You don't need to do this as you are already using single-quote-delimited strings.
Use bgsound tag for IE and
Use embed tag for Firefox.
The best way to play sound on a web page is through Flash. If you don't want to create a Flash movie just for playing a sound, there's a really nice library called SoundManager2 which allows you to play sounds directly through JavaScript (it creates Flash objects without you having to know about them).
Alternatively, you could use the new <audio>
tag, but it isn't supported on widely used and stoneage-like browsers such as Internet Explorer. So, if you need to support such browsers you should go with SoundManager2. Otherwise, use of the <audio>
tag is recommended, and, much nicer :)
<SCRIPT TYPE="text/javascript">
<!--
var filename="Welcome To The Club.mp3";
document.writeln ('<EMBED SRC="' + filename + '" AUTOSTART=TRUE loop=TRUE WIDTH=144 HEIGHT=60><P>');
// -->
</SCRIPT>
精彩评论