最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Giving background music to a webpage - Stack Overflow

programmeradmin1浏览0评论
   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?

   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?

Share Improve this question edited Jan 14, 2019 at 14:18 Matthieu Brucher 22.1k7 gold badges43 silver badges65 bronze badges asked Mar 22, 2010 at 10:56 ragrag 5152 gold badges7 silver badges15 bronze badges 8
  • 27 Eeeewwwww!!! Please, don't play music on your web pages. PLEASE! – Sinan Ünür Commented Mar 22, 2010 at 10:58
  • Makes me thankful my volume remains muted until I need it. – Andy E Commented Mar 22, 2010 at 11:02
  • Check this out: stackoverflow./questions/2472736/… – Kevin Commented Mar 22, 2010 at 11:11
  • Downvoted. This is one of the best ways to ensure that visitors leave your site quickly and never e back. – Cylon Cat Commented Mar 22, 2010 at 11:14
  • 6 @CylonCat -1 That's not a good reason to downvote the question! Any number of reasons why he might need to include background music. This is a technical question, not a style or design question. – Kirk Broadhurst Commented Mar 22, 2010 at 11:34
 |  Show 3 more ments

4 Answers 4

Reset to default 8

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 puters 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 remended, and, much nicer :)

    <SCRIPT TYPE="text/javascript">
<!-- 
var filename="Wele To The Club.mp3";

    document.writeln ('<EMBED SRC="' + filename + '" AUTOSTART=TRUE loop=TRUE WIDTH=144 HEIGHT=60><P>');

// -->
</SCRIPT>
发布评论

评论列表(0)

  1. 暂无评论