JavaScript code for a simple playback sound

Source: Internet
Author: User
1. The code is very short. You can play small audio files in WAV format. Please give your advice and don't laugh at me. Sound is the file name
  1. Function play (sound)
  2. {
  3. If (navigator. appname = "Microsoft Internet Explorer ")
  4. {
  5. VaR snd = Document. createelement ("bgsound ");
  6. Document. getelementsbytagname ("body") [0]. appendchild (SND );
  7. SND. src = sound;
  8. }
  9. Else
  10. {
  11. VaR OBJ = Document. createelement ("object ");
  12. OBJ. width = "0px ";
  13. OBJ. Height = "0px ";
  14. OBJ. type = "audio/X-WAV ";
  15. OBJ. Data = sound;
  16. VaR body = Document. getelementsbytagname ("body") [0];
  17. Body. appendchild (OBJ );
  18. }
  19. }

Instance:

  1. <HTML>
  2. <Head>
  3. <SCRIPT>
  4. Function play (sound)
  5. {
  6. If (navigator. appname = "Microsoft Internet Explorer ")
  7. {
  8. VaR snd = Document. createelement ("bgsound ");
  9. Document. getelementsbytagname ("body") [0]. appendchild (SND );
  10. SND. src = sound;
  11. }
  12. Else
  13. {
  14. VaR OBJ = Document. createelement ("object ");
  15. OBJ. width = "0px ";
  16. OBJ. Height = "0px ";
  17. OBJ. type = "audio/X-WAV ";
  18. OBJ. Data = sound;
  19. VaR body = Document. getelementsbytagname ("body") [0];
  20. Body. appendchild (OBJ );
  21. }
  22. }
  23. </SCRIPT>
  24. </Head>
  25. <Body>
  26. <Input type = "button" id = "test" onclick = "play('gold.wav ');" value = "test sound">
  27. </Body>
  28. </Html>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.