javascriptbackground-music

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 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?


Solution

  • 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.