开发者

How to handle browsers where JavaScript may be turned off?

Suppose I'm developing a web app that is heavily dependent on JavaScript and which uses AJAX for all the POST commands to store data, etc.

How should one deal with those situations where a user may have JavaScript turned off? Is it unreasonable to requ开发者_开发知识库ire that a user's browser has it turned on? Are there any best practices for these situations?


Use <noscript> tags to let users know that they cannot use the site without Javascript. It's not the best practice, but it is common at this point and most users without it will have experience being left out in the rain.

That said, if you can provide a version of the site that doesn't require Javascript (think the degraded version of gmail), that is clearly a superior solution. Without it, you will lose mobile surfers, plus blind folks and others with accessibility needs. And people who are too stubborn to use JS, but we don't care about them.


I personally think it is not that big of a requirement to expect people to have Javascript turned on.

If you do not want to support non-javascript, it is as simple as:

<noscript>
    This site works better with Javascript turned on.
</noscript>

Near the top.

If you still want to support users without Javascript, you can structure your Javascript so that if the JS isn't loaded, all of the forms still submit to the right place.

For instance, if you have a form:

<form action="" method="POST">
    <input type="text" name="name">
    <input type="submit">
</form>

Don't build the actual form with the idea that you are using Javascript. Instead, simply hook into the page to enhance the experience.

Regularly, the above form would submit to itself. With Javascript, you can hook in and make that Submit happen through AJAX instead of a new page load.


The key here is the concept of "graceful degradation". You want to offer the best possible experience you can despite not having javascript support. For AJAX, this usually means making full page requests rather than the lovely dynamic ones we're used to; POST will similarly have to submit through HTML and load a new page.

This obviously isn't as feature-rich, and displaying it well can be a challenge, but if you're looking for a best practice, that's the goal. It's also worth telling users via a noscript tag that they have javascript disabled (some people might have legitimately done this by accident), but turning off your entire site due to a lack of javascript is not friendly behavior.

Any site worth making is worth making accessible.


Depends on the site; something like Google Maps, for example, is clearly just not going to work without Javascript. So simply showing a noscript element politely mentioning that they will need it to view the site is fine.

Probably best to avoid any patronising "you need to get a new browser" messages; they may not be able to, for a number of reasons, or may simply have chosen to install NoScript or similar. According to Mozilla, NoScript has had nearly 70 million downloads; obviously a large proportion of those will be repeat downloads for the same person, but there could easily be 10 million people out there with it installed - that's not insignificant.

"Best practice" would, I guess, be to have a gracefully degraded version if possible. Up to you to decide whether that's worth the effort or if it's appropriate for your site.


You can send form data through action attribute. When JavaScript is turned on, remove the attribute by using removeAttribute or setAttribute. That way, when non-JS users use the browser, they can still submit the forms. However, in the backend code, you have to support non-JS as well, which is way more better than using <noscript> or no-js (Mordenizr).

Of course, there's still some drawbacks by doing this, for example you need to reload the whole page each time, which consumes a lot of time and memories.

So far, this is the best solution I can think of. Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜