JavaScript - An open source scripting language, it allows the creation of dynamic web page content.  Home Web Programming JavaScript How to Make Background Music in JS

How to Make Background Music in JS


1. Do an IFRAME, iframe src: music.htm

If you don't know how to make i frames, read this: Iframes Basics

2. Make new htm doc.

Write in it this code in <head>:

<bgsound id="PlayMusic">
<script type="text/javascript">
<!--
PlayMusic.src="music.mp3";
function OnOff() {
   if (PlayMusic.src=="")
   {
   PlayMusic.src="music.mp3";
   OnOffButton.value="Stop";
   } else {
   PlayMusic.src="";
   OnOffButton.value="Play";
   }
}
// -->
</script>

replace the file name in "music.mp3". its not must be mp3, its can be MID or WAV, but the best is MP3 or MID.
now, in the <body> but this:

<input type="button" name="OnOffButton" value="Stop" onClick="OnOff()">

and save it as "music.html
the html shod look loke this:

<html>
<head>
<bgsound id="PlayMusic">
<script type="text/javascript">
<!--
PlayMusic.src="music.mp3";
function OnOff() {
   if (PlayMusic.src=="")
   {
      PlayMusic.src="music.mp3";
      OnOffButton.value="Stop";
   } else {
      PlayMusic.src="";
      OnOffButton.value="Play";
   }
}
// -->
</script>
</head>
<body bgcolor="#000000" scrollamount="0">
<input type="button" name="OnOffButton" value="Stop" onClick="OnOff()">
</body>
</html>

Now, u have a web site background music without flash!!!



Author's URL: iCe
JavaScript - An open source scripting language, it allows the creation of dynamic web page content. More JavaScript Tutorials: Featured Materials | Fresh Materials | More JavaScript Tutorials at LearnPHP.org

Reader's comments
comments ocean seven November 06, 2011 says:
<object type="application/x-shockwave-flash" data="dewplayer.swf" width="1" height="1" id="dewplayer" name="dewplayer"> <param name="wmode" value="transparent" /><param name="movie" value="dewplayer.swf" /> <param name="flashvars" value="mp3=musique_fond.mp3&amp;autostart=1&amp;autoreplay=1" /> </object>
<script language="Javascript">

This code works on firefox , chrome etc.

Reply
comments ocean seven November 06, 2011 says:
<object type="application/x-shockwave-flash" data="dewplayer.swf" width="1" height="1" id="dewplayer" name="dewplayer"> <param name="wmode" value="transparent" /><param name="movie" value="dewplayer.swf" /> <param name="flashvars" value="mp3=musique_fond.mp3&amp;autostart=1&amp;autoreplay=1" /> </object>
<script language="Javascript">

This code works on firefox , chrome etc.

Reply
comments this code for IE only July 07, 2011 says:
it doesn't work Chrome and FF
Reply
comments rahul kumar April 30, 2011 says:
Hi. This code = error in Firefox...
and
PL z I told that how replace to this code
music.mp3

Please help me...

rahul100.designer@gmail.com

Reply
comments Harduj December 09, 2010 says:
Hello

This is working good
But my question is how can I play the sound continuously ?

pls Repl on ksit12345@gmail.com

Reply
comments DeryeN February 12, 2010 says:
Hi. This code = error in Firefox...
Please help me...

eryeffvii@hotmail.com (email/msn)

Reply
Add comments to "How to Make Background Music in JS"

Captcha