views:

7478

answers:

10

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

+1  A: 

Aaaaarrrrgh! The horror!

Nevermind, use Flash to play the sound.

SpliFF
+1 oh, the humanity, thanks for the laugh, and using flash, you can use the fine jQuery plugin to do this: http://plugins.jquery.com/project/sound
altCognito
Really, no need to involve Flash here. There are much simpler methods.
Noldorin
+2  A: 

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.

Darin Dimitrov
+21  A: 

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.

Noldorin
Note: Although I've given the direct answer, I do believe Mark Hurd is offering sound advice regarding not annoying the user.
Noldorin
+1 and -1 for answering the question :)
Mike Robinson
You may also experience browser compatibility issues with this style of playing sound. PLaying sound from a browser in a cross-browser compatible way is hard!
krosenvold
Nicely answered!
J-P
Isn't it possible to have access to the PC Speaker? (from which we hear BIOS beeps)
Nirmal
@Nirmal, no and FYI modern PC cases don't even have "buzzers" anymore (at least in the 3 last cases I bought).
AlexV
A: 

Sort of a Dupe..

Josh W.
Umn, voted down? It's the same question. He's right.
altCognito
A: 

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(){};
}
geowa4
+17  A: 

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.

Mark Hurd
I completely agree in this respect. I've provided the solution anyway, since that's what the asker was wanting, though it's wise to post this advice too.
Noldorin
This isn't the answer. This is advice, but in no way answers the question "How to make a beep in Javascript".
seanmonstar
Thank you for the advice. I took your suggestion and created a character count instead of a beep. The outcome was worth the abuse.
Slim
@seanmonstar - Absolutely right. Noldorin had already written up an excellent response (upvoted!). Never hurts to provide additional advice or alternate ideas when other folks have previously answered the question.
Mark Hurd
Is it really such a bad thing to make web forms work like windows forms? Windows forms frequently play a sound on an error condition.
quillbreaker
@quillbreaker: Yes, websites which play sounds are horrible and annoying. It is very common to open a website in a background tab as a sort of short-term bookmark. Having random stuff you aren't paying attention to beep at you is very annoying, especially if you are trying to watch a flash video or whatever. My expectations for web forms are vastly different from my expectations for windows forms...though beeping on windows forms is also usually something I don't appreciate.
Brian
There are some very good situations for using a beep. For example, what if the input is being entered by barcode? If the user is scanning many barcodes at a time (perhaps receiving items into inventory), they may not be looking at the screen at all. A beep will alert them to an invalid barcode, without forcing them to look up at the monitor every time they scan something.
limscoder
+3  A: 

"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: 

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.

Andrew
+1  A: 

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.

VME
+5  A: 

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.