3

I have this code and i don't know why it doesn't function

<html>
<head>
<title>Test</title>
<script type="text/javascript">
function StopBackgroundMusic()
{
var MusicObj = document.getElementById("BackgroundMusic");

MusicObj.data = "http://www.oreillynet.com/examples/oreilly/digitalmedia/2005/02/ableton_intro_0205_gtr.mp3";
MusicObj.autoplay = "false";
MusicObj.autostart = "false";


return MusicObj;
}
</script>
</head>
<body>


<OBJECT ID="BackgroundMusic" data="http://www.oreillynet.com/examples/oreilly/digitalmedia/2005/02/ableton_intro_0205_gtr.mp3" TYPE="audio/mpeg" height="0" width="0">
<PARAM NAME="autostart" VALUE="true">
<PARAM NAME="autoplay" VALUE="true">

</OBJECT>

<form> 


<input type='button' onclick='StopBackgroundMusic()' value='Stop music'/>

</form> 
</body>
</html>
4
  • 1
    It would help enormously if you would be more specific about the problem than to just say that this "doesn't function." Commented Feb 3, 2011 at 17:57
  • 1
    Why do you think that adding the autoplay and autostart properties to the OBJECT element should have any effect? Commented Feb 3, 2011 at 17:58
  • Why would switching the autostart or the autoplay to false stop the current playing of the music? Commented Feb 3, 2011 at 17:59
  • I try to stop the song but it has not any effect Commented Feb 3, 2011 at 18:00

2 Answers 2

3

Try setting data to null instead of setting the music URL again.

Another alternative is removing the object node when you press the stop button:

function StopBackgroundMusic()
{
    var MusicObj = document.getElementById("BackgroundMusic");
    MusicObj.parentNode.removeChild(MusicObj);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@joni: Then try removing the whole object. I never used the object tag for playing audio but I'm 95% sure there is no JavaScript interface to control the audio. Or it may depend on the plugin which plays the audio. If you want real control over the audio you might try the HTML 5 audio tag. But it doesn't work in IE.
function StopBackgroundMusic() { var MusicObj = document.getElementById("BackgroundMusic"); MusicObj.parentNode.removeChild(MusicObj); } It works. Thanks
0

does the element "BackgroundMusic" is encapsulated within a form element? if so try similar things like this one,

document.getElementById("[formID].BackgroundMusic");

if you use a form tag, give it an id,

<form id="music">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.