2

I have some functionality on my page that JavaScript is currently handling. I want to make my page compatible with non-Javascript users. Is there a way I can say "If JavaScript is enabled, use my JavaScript routine, otherwise do a Postback and let the server-side handle it?"

3 Answers 3

4

I have to presume that whatever triggers this postback or client-side behaviour (depending on javascript being available or not) is a html submit button?

If it is, I would attach the javascript handler for the client-side stuff to the button and have the handler return 'false' to prevent postback. When javascript is not enabled, the button will perform the standard html form function of triggering a postback and that will then let you do things server-side instead.

You can do a similar thing for links by having javascript run on the link click event to do client-side stuff but just follow the link in the event javascript is not available.

These methods are a bit more graceful in how they react to the browser not having javascript as they fall back to standard html form behaviour and require no code to detect javascript - it simply ignores your javascript and submits the form as it should.

Regarding checkboxes, be aware that the default behaviour in non-javascript enabled pages is not to post back on changes. If you change the state of a check-box, you always have to hit the submit button to postback, you simply can't do a postback on checkbox state change without javascript.

Several controls in asp.net (including drop down lists) have the option to auto-postback upon client change, this is done via javascript and if that is not available the only way to gracefully degrade is to have a button that the user clicks to force a standard html postback so your server can respond to the changed control state.

If you find you are struggling to have a coherent 'flow' in your UI if you don't have the auto-postback on checkboxes/combos etc then you might need to rethink your UI design and move to a more wizard-based layout.

I disable auto-postbacks on those types of things anyway as it is horrible for the user, which is a shame as you are going to great lengths to give them a usable experience when their browser is not using javascript.

Sign up to request clarification or add additional context in comments.

6 Comments

Nice, I think this is what I need. Let me play around with it a bit. Thanks a lot!
Hi Neil, can you show an example of how to do this with a CheckBox control? When I return false on the onchange javascript event, it doesn't actually change the checked state.
I'll add to the answer but I don't think you'll be able to do this.
I've added a bit to the answer. In short, you can degrade gracefully if you use auto-postback on checkbox changes but it may involve a rethink of the UI which is probably a good thing anyway as postback on those changes is simply not what the user expects and is horrible.
Neil, thanks for the addition. The reason I want an event to occur when they click a checkbox is because I want to enable or disable a required field validator for a textbox. The checkbox is an "I Don't Know" option.
|
0

You might try leveraging the noscript html tag...

http://www.w3schools.com/TAGS/tag_noscript.asp

this doesn't handle the postback stuff, but you might offer a control to display to fire server side code.

Comments

0

I recommend you read up on using unobtrusive JavaScript, which the solutions proposed abide by. The fundamental idea is that JavaScript and Ajax should extend your user experience rather than being a requirement. Your JavaScript code should hijack events like clicking a button and prevent the default behavior, since the script should handle the processing and responses in real time, within the browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.