javascript:渐进增强是否意味着ajax没有json?

发布于 2024-11-14 10:31:01 字数 475 浏览 3 评论 0原文

我在此处读到了这篇文章,其中讨论了 javascript 的渐进增强,作者提到:

首先,建立一个老式网站 使用超链接和表单来传递 信息到服务器。服务器 返回每个的全新页面 请求。

现在,使用 JavaScript 拦截这些链接和表单提交, 通过传递信息 改为 XMLHttpRequest。那么你可以 选择页面的哪些部分需要 进行更新而不是更新 整个页面。

我有点好奇这是否意味着在服务器端返回 html 标记而不是 json,这通常意味着在客户端构建标记?这种方法有缺点吗?

另外,我注意到当我禁用 Javascript 时,应用程序(例如 Facebook)看起来相当瘫痪(无法发布更新等)。这是否意味着它无法正确处理优雅降级?

I read this article here that talks about progressive enhancement for javascript and the author mentioned:

First, build an old-fashioned website
that uses hyperlinks and forms to pass
information to the server. The server
returns whole new pages with each
request.

Now, use JavaScript to intercept those links and form submissions and
pass the information via
XMLHttpRequest instead. You can then
select which parts of the page need to
be updated instead of updating the
whole page.

I'm a little curious if does that means returning html markups at the server side instead of json, which usually means building the markup on the client side? Is there a disadvantage for this approach?

Also, I notice applications, for instance Facebook, looks pretty crippled when I disabled Javascript (can't post updates etc.) Does that means that it does not handle graceful degradation properly?

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

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

发布评论

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

评论(3

极度宠爱 2024-11-21 10:31:01

渐进增强是否意味着ajax不需要json?

不,这绝对不是这个意思。如果禁用 JavaScript,则不会有 XMLHttpRequest,因此不会没有 ajax

现在,使用 JavaScript 拦截这些链接和表单提交,并通过 XMLHttpRequest 传递信息。

拦截链接和表单提交的 JavaScript 位可以自由更改请求的位置、URL 参数等,这意味着 ajax 化的 URL 不必与无 JavaScript 的 URL 相同。例如:

<a href="/some/page.html">linky</a>

可以被拦截并转换为 XMLHttpRequest,它实际上是针对

  • /some/page.json
  • /some/page.html?ajax= 1,或
  • /bibbidi/bobbidi/boo(不管怎样)

Does progressive enhancement means no json with ajax?

No, it most certainly does not mean that. If JavaScript is disabled, there is no XMLHttpRequest, so there is no ajax.

Now, use JavaScript to intercept those links and form submissions and pass the information via XMLHttpRequest instead.

The JavaScript bits that intercept links and form submissions can freely change where the requests are made, URL parameters, and so on, which means that ajaxified URLs don't have to be identical to JavaScript-less ones. For example:

<a href="/some/page.html">linky</a>

could be intercepted and turned into an XMLHttpRequest which is actually made to

  • /some/page.json, or
  • /some/page.html?ajax=1, or
  • /bibbidi/bobbidi/boo (for all that it matters)
初见你 2024-11-21 10:31:01

渐进增强意味着您使用可以在任何地方工作的代码启动页面,然后逐步添加该用户浏览器接受的功能。一个很好的例子是带有锚点的 ajax 类型功能。当页面加载时,您可以在 href 中使用 url,以便蜘蛛和非 javascript 浏览器仍然可以获得内容。但您还添加了一个 onclick 来执行 ajax 加载。这样,启用和禁用的客户端都可以获得最佳的行为。

Progressive enhancement means that you start the page with code that will work everywhere, and then progressively add functionality that is accepted by that user's browser. A good example of this is on ajax type functionality with anchors. When the page loads, you can use urls in the hrefs so that spiders and non-javascript browsers can still get the content. But you also add an onclick that does the ajax loading. That way both the enabled and disabled clients get the best behavior that they can.

狼亦尘 2024-11-21 10:31:01

从本质上讲,渐进增强意味着您首先将构建一个完全运行的“无 Javascript”网站放在优先地位和重要性,然后通过添加 Javascript 功能和 AJAX 来慢慢增强您的网站,同时保持“无 javascript”功能正常运行。

这只是为了让那些禁用 Javascript 的人能够正常访问和使用该网站。

Essentially, progressive enhancement means you place the priority and importance of building a fully working "no-Javascript" website first, then slowly enhance your website by adding the Javascript functionality and then AJAX, while keeping the "no-javascript" features working.

This is just to allow those who have Javascript disabled to access and use the site as per normal.

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