PDA

View Full Version : ASP.NET's AutoPostBack feature and graceful degradation of JavaScript


chump2877
08-22-2007, 05:07 AM
In my experience so far with ASP.NET, and as a JavaScript programmer, I have noticed that ASP.NET's "AutoPostBack" feature/property does not degrade particularly well if JavaScript is manually disabled in a JavaScript-capable browser (like IE7, FF, or any modern browser).

The only (albeit ugly) solution to this problem that I've heard of so far is to provide at least one Button control on a page with ASP.NET controls that utilize the AutoPostBack feature.

I have some issues with this solution.

This is not really "graceful" degradation of JavaScript (JS). In my opinion, a "graceful" degradation would involve the automatic and imperceptible delivery of different user experiences (depending on whether JS is available), without the user ever knowing that he/she is being delivered a unique experience. The idea is that a JS-user need not know about a non-JS user's experience, and vice versa. Thus, the addition of a Button control explicitly for use by non-JS users seems to be a clumsy solution (since the button is also visible to users who have enabled and are using JavaScript).

In short, I don't see a way to hide this button from browser users that have JS enabled. ASP.NET can detect if a browser supports JS (and, while this feature is serviceable, maybe it is not always reliable? -- which is potentially an even larger problem), but ASP.NET web applications do not appear to degrade well in the event that JS is disabled in a browser that supports JavaScript. Ideally, it would be nice if there was a way to bypass ASP.NET's "browser sniffing" techniques altogether (in this situation), and simply offer a simultaneous, alternate experience to non-JS users (without the browser detection).

Does anyone know of a solution to this problem that truly degrades gracefully?

Thanks.

chump2877
09-02-2007, 04:30 AM
By the way, I posted this a little prematurely before actually doing any testing....

You can make the button or buttons (used to submit the page form when JS is disabled and the AutoPostBack property is rendered useless) invisible to browsers that have JS enabled rather easily, as I found out.

Just include some JS in your page (or in a linked external file) that completely removes the button(s) from the DOM on page load (window.onload). BTW - I tried setting the CSS display property of the button to "none" (in my JS code), but that still didn;t eliminate extra spaces (caused by the button) in some browsers. Better to completely remove the node from the DOM, for browser uniformity.

So, when your page loads, either JS is enabled or disabled (obviously). If enabled, the browser recognizes the JS code and removes the extra (and unnecessary) button controls from your form. If disabled, the JS script never runs and the buttons provide an alternative to the AutoPostBack feature.

Voila, problem solved.