views:

311

answers:

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

+8  A: 

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.

Splash
Wow, I wouldn't have thought someone on stackoverflow would actually recommend `document.write()`-ing `bgsound` s
Felix
I am not *recommending* anything. I am simply giving him the answer to the question - why did it not work.
Splash
+1  A: 

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 :)

Felix
+1 for suggesting Flash. The really nice thing about it is that it can be easily blocked :)
0xA3
+1  A: 

Use bgsound tag for IE and
Use embed tag for Firefox.

ungalnanban
He is. That is *very* clear from the question.
David Dorward