How to play Sound on Form Load in Asp.net Using JavaScript



In this Example we will See When you want play some back ground Music on page load using java script. Create Script for that or else on button click you want to play music.

Create Webpage and Create JavaScript Code for Playing Music in background.
Create Folder "sound" in Web Application and Add .wav in that Folder.

<script type="text/javascript">
        var soundObject = null;
        function PlaySound() {
            if (soundObject != null) {
                document.body.removeChild(soundObject);
                soundObject.removed = true;
                soundObject = null;
            }
            soundObject = document.createElement("embed");
            soundObject.setAttribute("src", "sounds/sound.wav");
            soundObject.setAttribute("hidden", true);
            soundObject.setAttribute("autostart", true);
            document.body.appendChild(soundObject);
        }
        window.onload = PlaySound;
</script>

This is the Button Code if you want to Play Music on Button Click Event

<div>
      <input type="button" onclick="PlaySound()" value="Play Sound" />
</div>

Related Posts

Previous
Next Post »

1 comments:

comments

Thanks for comments.....