When is it OK to use Javascript and when not?

2020-05-31 04:19发布

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.

12条回答
聊天终结者
2楼-- · 2020-05-31 04:40

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.

查看更多
干净又极端
3楼-- · 2020-05-31 04:41

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.

查看更多
做个烂人
4楼-- · 2020-05-31 04:46

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.

查看更多
唯我独甜
5楼-- · 2020-05-31 04:48

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.

查看更多
叛逆
6楼-- · 2020-05-31 04:48

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.

查看更多
\"骚年 ilove
7楼-- · 2020-05-31 04:50

+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.

查看更多
登录 后发表回答