Warm tip: This article is reproduced from stackoverflow.com, please click
audio javascript browser beep

How do I make JavaScript beep?

发布于 2020-03-27 15:41:51

I want my web page to beep whenever a user exceeds the maximum character limit of my <textarea>.

Questioner
Slim
Viewed
43
35.5k 2017-01-29 10:01

It's not possible to do directly in JavaScript. You'll need to embed a short WAV file in the HTML, and then play that via code.

An Example:

<script>
function PlaySound(soundObj) {
  var sound = document.getElementById(soundObj);
  sound.Play();
}
</script>

<embed src="success.wav" autostart="false" width="0" height="0" id="sound1"
enablejavascript="true">

You would then call it from JavaScript code as such:

PlaySound("sound1");

This should do exactly what you want - you'll just need to find/create the beep sound yourself, which should be trivial.