I want to my web page to beep whenever a user exceeds the maximum character limit of my textarea.
There's no crossbrowser way to achieve this with pure javascript. Instead you could use a small .wav file that you play using embed or object tags.
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.
Here is an example from this page (cleaned up slightly).
<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.
You need a sound file to be served from somewhere. Here's the code from Scriptaculous's Sound library:
//Default:
<embed style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>
//For Gecko:
if(Prototype.Browser.Gecko && navigator.userAgent.indexOf("Win") > 0){
if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 }))
Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>');
else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('Windows Media') != -1 }))
Sound.template = new Template('<object id="sound_#{track}_#{id}" type="application/x-mplayer2" data="#{url}"></object>');
else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('RealPlayer') != -1 }))
Sound.template = new Template('<embed type="audio/x-pn-realaudio-plugin" style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>');
else
Sound.play = function(){};
}
Please don't play a sound! Use a character countdown (see the stack overflow comments) or visually change the form when the number of characters are exceeded along with displaying an error message.
Not only will this be more prominent (who says the user has their speakers on, or is in an environment in which they can hear it?) but it will reduce some annoyance on your user's end.
Anything that automatically makes noise and gives you no control has no place in the browser, IMHO.
"Not only will this be more prominent (who says the user has their speakers on, or is in an environment in which they can hear it?) but it will reduce some annoyance on your user's end."
Who says the user can see the screen? They may be blind or partially sighted and accessing the page through a screen reader in which case a sound cue may be more appropriate - which is indeed why I am searching for a robust and cross-browser way of playing certain sound files under very specific conditions to assist such users. I'm already using ASP.NET AJAX so I would prefer to use that library if I can.
A beep can be useful in debug situations. Being able to control which sound is played can quickly allow you to 'listen' to the work flow, so this solution is very useful. The point is clear however, this would not belong on a Production/Customer site.
What if the person is keying from another page and not looking at the screen? They'll never see the visual cue, and keep typing. They need a beep to stop.
Lots of medium-level (or inferior senior-level) developers here are over-generalizing. Yes, it's usually bad form to play audio. However, there are definitely some perfectly good production use cases for it.
If somebody hasn't written enough apps to appreciate the need, they should say that they haven't come across a valid need, not instruct others that it is never appropriate for production apps just because they haven't come across the need in their vast, yet hereby proven-inadequate, experience.