什么时候可以使用Javascript,什么时候不可以?

发布于 2024-09-02 18:31:21 字数 185 浏览 12 评论 0原文

不使用太多 javascript/jquery 是个好习惯吗?我们应该尽可能避免它(为了良好的可访问性)吗?

什么时候可以使用 JavaScript,什么时候不能在网页设计和开发中使用 JavaScript?在什么场景、什么条件下?

更新:

我询问的是有关公共网站的信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

傲性难收 2024-09-09 18:31:22

使用 javascript,我们可以与用户建立客户端/服务器关系,从某种意义上说,我们可以使用他们的 CPU 能力来构建页面并从我们的服务器释放一些资源。我们提供代码和数据,他们的浏览器将它们组合在一起,就这样。 :)

真挚地
巴巴克

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

停滞 2024-09-09 18:31:22

好吧,我不同意你的观点,即不要经常使用 java-script 和 jquery。
现在,浏览器已与 javascript 兼容。如果您必须开发一个需要验证的网站,您必须更喜欢 javascript(用于客户端验证)。

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

请恋爱 2024-09-09 18:31:22

您的网站应该优雅地降级,即使只有一个原因:NoScript。该 Firefox 扩展依赖于白名单来允许网站运行 Javascript。这意味着我以前从未访问过的网站将不允许运行 Javascript。这是防止大量网络钓鱼攻击和跨站脚本 (XSS) 攻击的好方法。

网站必须首先赢得我的信任。是的,Ebay 和 Amazon 可能严重依赖 Javascript,但像 www.buyviagracheap.com 这样的网站可能不会。

如果禁用了 Javascript,您没有什么可展示的,我会在您说“但是...”之前快速按“返回”,然后再也不会回来。

但是,如果我喜欢我所看到的内容,我可能会为您的网站启用 Javascript 并改善我的体验。因此,无需在禁用 Javascript 的情况下提供完整替代功能。

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.

丿*梦醉红颜 2024-09-09 18:31:21

我必须恭敬地不同意那些说你不应该使用 JavaScript,或者谨慎使用它,或者让它优雅地降级的发帖者。

原因是现在绝大多数人都启用了 JavaScript,并且喜欢它可以从网站提供的类似桌面的体验。真的,谁没有启用 JavaScript?人们表现得好像这是一个具有统计意义的群体。它不是。

不使用 JavaScript 有点像对变量大小吹毛求疵(哦,我可以在这里使用 16 位整数而不是 32 位来节省一些内存)。除非你正在为数十万人做一些怪物项目,否则你花在让网站优雅降级上的时间的投资回报率实际上是积极的,否则你应该随心所欲地使用 JavaScript。因为禁用它而无法访问它的两个人是偏执狂,而且可能不是您想要作为客户的那种人。

只是我的2分钱。

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.

相思碎 2024-09-09 18:31:21

对专家先生所说的一切+1。

还要补充一件事:网站的关键功能依赖于 JavaScript 对可访问性有好处。如果用户浏览器中禁用了 JS,他们仍然应该能够提交所有表单、单击所有按钮等等。 您的网站必须在没有 JavaScript 的情况下正常降级。

表单的一个注释:

如果可能,请使用 Hijax 方法提交表单。让它们使用传统的页面刷新来工作,然后使用 JavaScript“劫持”表单提交并使用 AJAX 来完成。如果客户端禁用了 AJAX,表单仍然可以正常工作。

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

晨与橙与城 2024-09-09 18:31:21

在我说话之前,让我先澄清一下,所有这些都是相对的 - 一切都与您的目标受众有关。如果您的大多数目标受众是非洲的残疾人,而我的目标受众是韩国的游戏玩家,那么答案可能处于相反的两端。

首先,查看目标受众中启用 JS 与禁用 JS 的比例。对于一般网站来说,该比例是 100:1。

其次,考虑带宽。 jQuery 缩小并压缩后大小为 24K。但是所有浏览器都可以正常使用 gzip 压缩的内容吗?选择正确的 UI 框架或选择是否使用一个框架,具体取决于目标受众的带宽。如果您的目标受众是大量使用宽带计划的年轻人,那么即使框架大小达到兆字节,他们也不会抱怨。但是,当您的网站针对某个国家的偏远村庄进行救援工作或教育计划或其他活动时,请避免使用此类框架 - 他们几乎无法访问互联网。

第三,对于可访问性,有两件事很重要:

  1. 任何人都应该能够看到/听到/知道您网站中的内容。
  2. 任何人都应该能够执行您网站中的所有重要功能。

一旦你使用最小分母技术为你的目标受众处理好这些问题,你就可以随时使用 javascript 来美化事物并增强现有的基本功能(自动完成、AJAX 提交等......)

总而言之,优雅地降级。

抛开可访问性不谈,我不同意我们应该在有人禁用 JS 的情况下优雅地降级!

对于带有浏览器的台式机,说当人们没有启用 javascript 时,您的网站应该优雅地降级就像说您的游戏引擎应该优雅地降级到 DirectX 6,因为有些人使用 Windows 95。 。没有意义了。请注意不再这个词。当 JavaScript 仅出现在 50% 的浏览器上并且它是一项新兴技术时,它曾经是有意义的。

有人有充分的理由解释为什么我的 3D 游戏应该能够正常降级并使用 DirectX 6 吗?它没有实际意义。有意义的是,我的游戏在 Windows 7 上使用 DirectX 11,但优雅地降级,并在 Vista 中使用 DX10,甚至在 XP 中使用 DX9。

来吧..看看一些统计数据。启用 JS 与禁用 JS 的比例约为 100:1

如果您的 80% 的受众在移动设备上使用某些即将推出的 Web 浏览器(其 JS 实现不稳定)来查看您的网站,那么整个情况就会再次发生变化。

如果您的大多数目标受众/设备都启用了 JS,请充分利用它。如果他们没有,那就不要。您只需为他们提供他们可以使用和看到的内容。

总会有少数人,但如果有一个先决条件来查看一个网站并且它相当普遍,他们应该安装/启用它,否则对他们来说太糟糕了。您当然不希望目标受众中有偏执狂。

最终,只有您才能获得帮助您决定应该使用 JavaScript 的程度的信息。它始终由您的目标受众及其设备决定。

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.

情未る 2024-09-09 18:31:21

如果您正在开发一个简单的网站,那么您应该只使用 JavaScript 来增强用户体验,对于那些没有启用它的人来说,它应该会优雅地降级。如果您的网站以内容为中心,那么您应该这样对待它:内容第一,基于 JavaScript 的花哨功能第二。如果没有启用 JavaScript,不应该有任何一项必需的功能无法工作。

但是,如果您正在制作一个网络应用程序,那就尽情发挥吧。网络应用应该使用JavaScript,因此迎合那些禁用它的人是没有意义的;如果他们确实想使用您的产品,他们将启用 JavaScript(或使用不同的设备)。在没有启用 JavaScript 的情况下让它工作是不值得的。这就像争论你不应该为你的视频游戏提供良好的图形,因为低端计算机将无法运行它:真正想玩你的视频游戏的人会升级他们的机器。

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.

℡Ms空城旧梦 2024-09-09 18:31:21

MHR 的回答是“始终如此,只要它能够正常降级”,这是一个很好的基线。我想补充一点,当 Javascript 提供没有服务器端等效功能的应用程序功能(您的网站是一个“网络应用程序”而不是纯粹的信息性网站)时,可以做出合理的例外。例如,“优雅降级”作为规则不应阻止您构建基于 Web 的绘图工具(如果它优雅地降级为表单和服务器端功能,那么充其量,这将是难以使用的繁重)。但是,它应该阻止您要求 Javascript 来访问该绘图工具向内容创建者以外的受众发布的任何内容(因为内容创建者,不言而喻,已经接受了使用该网站需要 Javascript) )。

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

暗地喜欢 2024-09-09 18:31:21

谨向 J. Nielsen 先生表示敬意:就进化和进步而言,设计中的保守主义确实毫无意义。当 Flash 第一次出现时(随着 ActionScript 的第一个版本,真正的外卖编程)出现了许多嘈杂的动画界面,其中大多数在可用性方面几乎无法操作。但事实是,从所有这些未来主义实验中,用户界面方面已经出现了一些质的改进。

对于 javascript,也发生了类似的事情:原本只是一个小部件的东西变得越来越流行,甚至超越了表示层来处理一些业务逻辑:RIA(富互联网应用程序)逐渐依赖 javascript 来提供用户体验。
使用还是不使用javascript?

我认为,在一个不断发展的全新世界中,每一种旨在提高可用性和交互性的工具都会受到欢迎,而这远远超出了一个清晰的形状、方向或只是一个简单的计划。

其他人在这里所说的完全正确:不值得考虑仍然禁用 JavaScript 的一两个,因为游戏制作人并不太关心您的计算机是否发臭并且他们不断突破图形的限制。多亏了这一点,我们不再玩 PACMAN(除非 Google 愿意),我们可以享受《刺客信条》了。

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.

风吹雪碎 2024-09-09 18:31:21

NASA 是一个很好的例子,说明如何不在公共网站中使用 JavaScript - 他们似乎正在使用 JS提供浏览器特定的样式表,导致 JS 禁用时出现无样式的混乱。

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.

南汐寒笙箫 2024-09-09 18:31:21

JavaScript 的主要用途是编写嵌入或包含在 HTML 页面中的函数,并与页面的文档对象模型 (DOM) 进行交互。此用法的一些简单示例包括:

  1. 通过编程控制新窗口的大小、位置和属性来打开或弹出一个新窗口。

  2. 验证 Web 表单输入值,以确保它们在提交到服务器之前会被接受。

    验证 Web 表单输入值,以确保

  3. 当鼠标光标移到图像上时更改图像:此效果通常用于吸引用户注意显示为图形元素的重要链接。

由于 JavaScript 代码可以在用户浏览器本地运行(而不是在远程服务器上),因此它可以快速响应用户操作,使应用程序感觉响应更快。

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.

找个人就嫁了吧 2024-09-09 18:31:21

在公共网站上,只要您的网站包含的信息仍然可用并且对于没有 JS 的人来说可用性仍然很好,就可以使用 JavaScript。

如果您处于更受控制的环境中,例如公司内部网或其他环境,您可能会更多地依赖 JavaScript。

此外,如果您的网站更多的是一个应用程序而不是一个文档,并且如果没有 JS,它的功能就不可能实现,那么您当然可以使用它。

您也可以接受这样一个事实:有些人在使用您的网站时会遇到问题,但无论如何都严重依赖 JS。那是你的选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文