I'm not sure about a javascript solution, but what happens if the end user has javascript turned off?
In a web app
you should try to make no assumptions about client side validation of input or requests. You should probably try to set some flag with the users session. Just an idea off the top of my head.