When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Business Systems Analyst - Clearing - SQL Server - ASP - VB (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Published: Sunday, October 11, 1998

Playing a Sound using JavaScript in Response to an Event


Have you ever wanted to play a sound when a user performed a certian action? This is not too difficult to do using JavaScript. What you need to do first, is embed your sound. You can do that by using the following code:

<EMBED NAME="MySound" SRC="MyWavFile.wav" AUTOSTART="false" HIDDEN="true">

- continued -

To play the sound, employ the following function:

<SCRIPT LANGUAGE="JavaScript">
      var bolDocumentLoaded = false;

      function PlaySound(mySound) {
           if (bolDocumentLoaded) {
                if (navigator.appName = "Netscape")
                      document.embeds[mySound].play(false);
                else
                     document.embeds[mySound].Run();
           }
      }

</SCRIPT>


You'll notice that we don't want to let the sound be played until the entire document is loaded. This ensures that we have fully downloaded the sound before trying to play it. To let JavaScript know that we've finished downloading the page, we need to code for the ONLOAD event. You will want to add this simple line inside your BODY tag:

<BODY ONLOAD="bolDocumentLoaded=true;">

Finally, to get the sound to play you just need to make a call to PlaySound, passing in the sound you want to play. So, if we had a button that you wanted to have a sound played when clicked, you could write this:

<FORM>
      <INPUT TYPE="BUTTON" NAME="btnSoundButton" ONCLICK="PlaySound('MySound');">
</FORM>

Happy Programming!


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers