views:

428

answers:

13

Is it good practice not to use much javascript/jquery? Should we avoid it as much as possible (for good accessibility)?

When is it OK to use JavaScript and when is it not in web design and development? In what scenarios and with what conditions?

Update:

I'm asking regarding public websites.

A: 

The primary use of JavaScript is to write functions that are embedded in or included from HTML pages and interact with the Document Object Model (DOM) of the page. Some simple examples of this usage are:

  1. Opening or popping up a new window with programmatic control over the size, position, and attributes of the new window.

  2. Validation of web form input values to make sure that they will be accepted before they are submitted to the server.

  3. Changing images as the mouse cursor moves over them: This effect is often used to draw the user's attention to important links displayed as graphical elements.

Because JavaScript code can run locally in a user's browser (rather than on a remote server) it can respond to user actions quickly, making an application feel more responsive.

Mr.Expert
This looks like it was cribbed from an article in written in 1996 explaining JavaScript as implemented in Netscape 3.
Tim Down
Ah: it was cribbed from Wikipedia. I was close.
Tim Down
+12  A: 

+1 to everything Mr. Expert said.

One more thing to add: it is not good for accessibility to have critical functions of your website rely on JavaScript. If JS is disabled in the user's browser, they should still be able to submit all forms, click all buttons, et cetera. Your website must degrade gracefully in the absence of JavaScript.

One note for forms:
Where possible, use the Hijax approach to submitting forms. Make them work using traditional page refreshes, and then use JavaScript to "hijack" the form submission and do it with AJAX instead. If the client has AJAX disabled, the forms will still work fine.

GlenCrawford
+1 for Hijax!!!
nico
"Your website must degrade gracefully in the absence of JavaScript."I agree.
Shahin
A: 

On a public website it's ok to use JavaScript as long as the information that your site contains is still available and usability is still good for people without JS.

If you're in a more controlled environment like a companies intranet or something, you can maybe rely a lot more on JavaScript.

Also if your site is more an application than a document and it's functionality is just not possible without JS, you can of course use it.

You can also just accept the fact that some people will have problems using your site but rely heavily on JS anyway. That's your choice.

Techpriester
+17  A: 

I have to respectfully disagree with the posters that say that you shouldn't use JavaScript, or use it sparingly, or have it degrade gracefully.

The reason is that the vast majority of people nowadays has JavaScript enabled and appreciates the desktop-like experience it can provide from a website. Really, who doesn't have JavaScript enabled? People act as if this is a statistically significant group. It is not.

Not using JavaScript is a little bit like nitpicking about variable sizes (oh, I can use a 16-bit integer here instead of 32-bit to save some memory). Unless you are doing some monster project for hundreds of thousands of people, where the ROI of the time you spend on making your website degrade gracefully is actually positive, you should use JavaScript as freely as you like. The two people that can't access it because they disabled it are paranoid and probably not the kind of people you want as customers anyways.

Just my 2 cents.

Alex
Amen, +1. As long as you show a clear apologetic message to users who choose to disable javascript, it's **fine** to rely on JS (with a good framework in support -- jquery, dojo, google closure, ...) for your presentation layer.
Alex Martelli
It's not as much a matter of how many people disable Javascript, as of how many can't *enable* or fully use it. The web is being consumed on more and more diverse devices, including very low-end widgets that barely do more than raw text and/or have input mechanics that may not play nice with things like mouseovers. This may or may not be a significant number of people, depending on your site's focus.
deceze
Also, remember that the spiders of search engines may have problems in indexing dynamically loaded content. That is a big point to consider. Remember, the web is content first, whistle and bells later.
nico
content for search engines or the raw text is only important for content-focused sites. web apps can be treated totally differently.
seanmonstar
Alex, I'd agree with you if we were talking about web applications, but for basic websites, all functionality absolutely *must* gracefully degrade. The problem is not people that willingly disable JavaScript (that population is negligible); the problem is people who use devices or browsers that do not *support* JavaScript. Also, you'd annoy the hell out of people that use NoScript. :)
musicfreak
If it doesn't degrade gracefully, your website is probably not very SEO friendly. Furthermore, it might be easier to do test driven development if it works without javascript.
Nicolas Viennot
“Really, who doesn't have JavaScript enabled?” — If you’re asking, then you don’t know. You should at least try to find out what your target audience supports.
Paul D. Waite
No Paul, this was a rhetorical question as you can tell from both the context and my following assertion that people without JavaScript are a statistically insignificant group in comparison to the internet population.
Alex
+2  A: 

mhr's answer, "Always, as long as it degrades gracefully", is a good baseline. I would add that reasonable exceptions can be made where Javascript provides application functionality (your site is a "web app" rather than purely informational) that has no server-side equivalent. So for example, "graceful degradation" as a rule should not prevent you from building a web-based drawing tool (which would be, at best, unusably onerous if it degraded gracefully to forms and server-side functionality). It should, however, prevent you from requiring Javascript to access any content which that drawing tool publishes to an audience other than the content creator (because the content creator, self-evidently, has already accepted that Javascript is required for their usage of the site).

eyelidlessness
+5  A: 

Before I say anything, let me make it clear that all of this is relative - its all about YOUR TARGET AUDIENCE. The answer can be on opposite ends of the spectrum if majority of your target audience is disabled people in Africa and my target audience is gamers in South Korea.

First, look at the ratio of JS enabled vs JS disabled in your target audience. For an average website, it is 100:1.

Second, consider bandwidth. jQuery minified and gzipped is 24K. But do all browsers work properly with gzipped contents? Choose the right UI framework or choose whether to use one at all, depending on your target audience's bandwidth. If your target audience is young people with heavy-usage broadband plans, they won't complain if the framework is megabytes in size. But when your website targets remote villages in some country for a relief effort or educational program or something then avoid such frameworks - they can barely get access to the Internet.

Third, for accessibility, two things are important:

  1. Anyone should be able to see/hear/know the contents in your website.
  2. Anyone should be able to perform all important functions in your website.

Once you take care of these using the minimum denominator technologies for YOUR target audience, you can always use javascript to pretty up things and enhance existing basic functions (auto complete, AJAX submit, etc...)

To sum it up, gracefully degrade.

Accessibility aside, I don't agree that we should gracefully degrade in the case where someone has JS disabled!

For desktops with browsers, saying that When people don't have javascript enabled, your website should degrade gracefully is like saying Your game engine should gracefully degrade to DirectX 6 because some people use Windows 95.. Doesn't make sense anymore. Note the word anymore. It used to make sense when JavaScript was only there on 50% of browsers and it was an emerging technology.

Anyone have any good reason why my 3D game should be able to degrade gracefully and use DirectX 6? Its moot. What DOES make sense is, my game uses DirectX 11 on Windows 7 but degrades gracefully and uses DX10 in Vista or even DX9 in XP.

Come on.. look at some stats. JS enabled to Disable ratio is like 100:1

Again the whole thing changes if 80% of your audience uses some upcoming web browser in a mobile device with shaky JS implementation to see your website.

If majority of your target audience/device has JS enabled, use it well. If they don't have, then don't. You just have to give them what they can use and see.

There will always be a minority, but if there is a pre-requisite to see a website and it is fairly widespread, they should have it installed/enabled or else its too bad for them. You certainly don't want paranoids in your target audience.

End of the day, only you will have the information that will help you decide how much you should use JavaScript. It is always dictated by your target audience and their devices.

Senthil
I agree, we have to draw the line somewhere. If the client doesn't support x, y or z then it might well be their responsibility to provide an alternative.
CurtainDog
+3  A: 

If you are developing a simple website, then you should only use JavaScript to enhance the user experience, and it should degrade gracefully for those that do not have it enabled. If your website is content-centric, then that's how you should treat it: content first, JavaScript-based bells and whistles second. There should not be a single piece of required functionality that does not work without JavaScript enabled.

However, if you are making a web application, then go nuts with it. Web apps are supposed to use JavaScript, so it doesn't make sense to cater to people who have it disabled; if they really want to use your produce, they will enable JavaScript (or use a difference device). It's not worth all the trouble making it work without JavaScript enabled. That's like arguing that you should not give your video game good graphics because lower-end computers will not be able to run it: the people who really want to play your video game will upgrade their machine.

musicfreak
A: 

Hello,

With javascript we can have a client / server relationship with users, in the sense that we can use their cpu power to build the page and free some resources from our servers. we deliver the code and data and their browsers put them together and it is as it should be. :)

Sincerely Babak

Bandpay
Huh? How's it freeing any server resources?!
deceze
Bandpay
A: 

well i am not agree with your point that not to use java-script and jquery regularly. now a day browser are compatible with javascript.And if you have to develope a website in which validation is required u have to prefer javascript(for client side validation).

subodh
A: 

Well!!!!Javascript is an easy-to-use programming language that can be embedded in the header of your web pages. It can enhance the dynamics and interactive features of your page by allowing you to perform calculations, check forms, write interactive games, add special effects, customize graphics selections, create security passwords and more.

it is most commonly used as a client side scripting language. This means that JavaScript code is written into an HTML page. When a user requests an HTML page with JavaScript in it, the script is sent to the browser and it's up to the browser to do something with it.

.JS will be very useful on client side programming like pop-up display,position changing etc.it will be very useful in the cases like not hitting the server,or not refreshing the page quite often.

but javascript is not a secured language as like java(Your code is readable and it can be modified by anyone),To avoid to write a lot of code on the server-side for validations, It would suggest you a simple method that will help you code faster. It is called the cheap return method.

Alright enough don’t-use cases. There are many more circumstances where using JavaScript is the only option. How about events? JavaScript is the only language available for having your website communicate with the browser and watch for events: clicks, double clicks, mouse enters, mouse exits, key presses, browser window sizing… the list goes on. If you need access to those events, you are in JavaScript territory.

lavi
+1  A: 

With all my respect to Mr J. Nielsen: Conservativism in design can be really senseless in terms of evolution an progress. When flash first appeared (with the first versions of actionscript, really take-away programming) a lot of noisy-glossy over animated interfaces raised, most of them almost impossible to operate in terms of usability. But the fact is that, form all those futuristic experiments, some qualitative improvements have arisen in terms of UI.

With javascript something similar happened: what was a merely widget is becoming more and more popular even transcending the presentation layer to handle some business logic: the RIA (Rich Internet Applications) are gradually relying on javascript for the user experience. To use or not to use javascript?

I think every tool oriented to improve usability and interaction is welcome in a brand new world that evolves continuously and that is way too far to have a sharpen shape, direction or just a simple plan underneath.

What others are saying here is completely true: it is not worth thinking of the one or two that still have javascript disabled, as game producers don't care too much if your computer stinks and they keep pulling the limits of graphics. Thanks to this, we are no longer playing PACMAN (only if Google wants) and we can enjoy Assassins Creed.

Moustard
Hey, speak for yourself, I've played Pac-Man far more in the last year (in the last week, even) than I've played just about any other game. :)
Charles Boyung
Yeah! Pac-Man wasn't a good example as it is never-ending classic...
Moustard
A: 

Your site should degrade gracefully, if only for one reason: NoScript. That Firefox extension depends on whitelisting to allow sites to run Javascript. That implies that sites that I've never visited before will not be allowed to run Javascript. It's a good way to prevent a lot of phishing attacks and cross site scripting (XSS) attacks.

Sites have to earn my trust first. Yes, Ebay and Amazon may heavily depend on Javascript, but a site like www.buyviagracheap.com may not.

And if, with Javascript disabled, you have nothing to show for, I'll press "back" quickly before you can say "but...", and never come back.

If, however, I like what I see, I may enable Javascript for your site and improve my experience. So there's no need to provide full alternative functionality with Javascript disabled.

bart
+1  A: 

NASA is a great example of how not to use JavaScript in a public website - they appear to be using JS to serve browser specific style sheets resulting in an unstyled mess with JS disabled.

Colonel Sponsz
Oh my God! How on Earth did they end up doing that?
nico
Given it's NASA I'm not too sure being on Earth was their main concideration.
Colonel Sponsz