设计一个支持和不支持 javascript 脚本的网站

发布于 2024-08-01 12:58:05 字数 371 浏览 11 评论 0原文

好吧,我知道在禁用 JavaScript 的情况下,您的网站能够正常工作非常重要。

在我看来,开始考虑如何设计此类网站的一种方法是在主页上检测 javascript,如果未启用它,则重定向到不使用 javascript 代码并使用纯 html(例如 gmail)的另一个网站版本(例如 gmail)

。例如,请考虑网页对话框上的 X(关闭按钮)。 如果在没有任何 javascript 干扰的情况下按下 X 会导致向服务器发送请求,并且在服务器端我们下次渲染页面时隐藏该对话框,并且我们还将 javascript 函数绑定到链接的 onclick ,以防万一启用 JavaScript 后,它将立即隐藏对话框。

你觉得这怎么样? 您将如何设计一个网站来支持两者?

Okay i know that it's important for your website to work fine with javascript disabled.

In my opinion one way to start thinking about how to design such websites is to detect javascript at the homepage and if it's not enabled redirect to another version of website that does not javascript code and works with pure html (like gmail)

Another method that i have in mind is that for example think of a X (close button) on a dialog box on a webpage. What if pressing the X without any javascript interference lead to sending a request to the server and in the server side we hide that dialog next time we are rendering the page, And also we bind a javascript function to onclick of the link and in case of the javascript enabled it will hide the dialog instantly.

What do you think of this? How would you design a website to support both?

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

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

发布评论

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

评论(6

辞别 2024-08-08 12:58:05

您的目标是渐进增强。 为此,我首先设计不使用 JavaScript 的网站,一旦可以运行,就开始通过 jQuery 等库添加 JavaScript 事件,以便网站的行为与演示完全分开。 通过这种方式,您可以为那些在浏览器中启用了 JavaScript 的人和没有启用 JavaScript 的人提供更高级别的功能和完善。

What you're aiming for is progressive enhancement. I'd go about this by first designing the site without the JavaScript and, once it works, start adding your JavaScript events via a library such as jQuery so that the behaviour of the site is completely separate from the presentation. This way you can provide a higher level of functionality and polish for those who have JavaScript enabled in their browsers and those who don't.

等你爱我 2024-08-08 12:58:05

最好的方法是设计一个无需 JS 也能正常工作的页面。 然后添加一个<脚本> 底部的块 包含如下代码的部分:

window.onload = function() {
    // Do DOM manipulations to add JS functionality here.  For instance...
    document.getElementById('someInputField').onchange = function() {
        // Do stuff here that you can't do in HTML, like on-the-fly validation
    }
}

研究 jQuery 示例。 他们展示了很多这样的东西。 这称为“不引人注目的 JavaScript”。 谷歌一下可以找到更多例子。

编辑:上面的 jQuery 版本是:

$(document).ready(function() {
    // Do DOM manipulations to add JS functionality here.  For instance...
    $('#someInputField').change(function() {
        // Do stuff here that you can't do in HTML, like on-the-fly validation
   });
});

我添加这个只是为了显示 jQuery 相对于标准 DOM 操作的详细程度。 window.onload 和 document.ready 之间存在细微差别,这在 jQuery 文档和教程中进行了讨论。

The best way is to design a page that works adequately without JS. Then add a <script> block at the bottom of the <body> section with code like this:

window.onload = function() {
    // Do DOM manipulations to add JS functionality here.  For instance...
    document.getElementById('someInputField').onchange = function() {
        // Do stuff here that you can't do in HTML, like on-the-fly validation
    }
}

Study the jQuery examples. They show lots of things like this. This is called "unobtrusive JavaScript". Google for that to find more examples.

EDIT: The jQuery version of the above is:

$(document).ready(function() {
    // Do DOM manipulations to add JS functionality here.  For instance...
    $('#someInputField').change(function() {
        // Do stuff here that you can't do in HTML, like on-the-fly validation
   });
});

I added this just to show the lower verbosity of jQuery vs. standard DOM manipulation. There is a minor difference between window.onload and document.ready, discussed in the jQuery docs and tutorials.

念三年u 2024-08-08 12:58:05

换句话说,让您的网站在禁用 JavaScript 的情况下运行并不重要。 禁用 JavaScript 的人是想侵入你的网站的人,他们不值得正确导航。 不要在他们身上浪费你的精力。 大家都知道,如果没有 JavaScript,网络就无法冲浪。

您唯一需要注意的是您的表单:永远不要相信 JavaScript 中的过滤器,始终在服务器端再次过滤它,永远!

In terms, it is not important to make your site work with JavaScript disabled. People who disable JavaScript are people who want to hack bugs into your site, they don't deserve to navigate it correctly. Don't waste your efforts with them. Everybody know the Web is unsurfable without JavaScript.

The only thing you have to be careful is about your forms: Don't ever trust filters in JavaScript, Always filter it again on server-side, ALWAYS!

冷月断魂刀 2024-08-08 12:58:05

使用渐进增强,研究 jquery 来理解它。 你需要一些时间才能理解它。 例如你的想法:

检测主页的 javascript
如果未启用重定向到
网站的另一个版本
不是 javascript 代码并且适用于
纯html

如何检测 javascript 是否被禁用? 显然,不是使用 javascript...

您的想法是错误的:最基本的版本必须是默认版本,然后,如果您检测到更高级的功能,则可以使用它们。

尽可能避免针对不同的弓箭手/功能使用单独的版本。 保持所有版本同步和最新需要做很多工作。

一些可以帮助您入门的好资源:

Use Progressive Enhancement, study jquery to understand it. It takes some time till you get your head around it. For example your idea:

to detect javascript at the homepage
and if it's not enabled redirect to
another version of website that does
not javascript code and works with
pure html

how would you detect if javascript is disabled? not with javascript, obivously...

you're thinking the wrong way round: the most basic version has to be the default version, and then, if you detect more advanced capabilities, you can use them.

Try to avoid separate versions for different bowsers/capabilities for as long as you can. It's so much work to keep all versions in sync and up-do-date.

Some good ressources to get you started:

万水千山粽是情ミ 2024-08-08 12:58:05

通常的方法是所谓的渐进增强

基本上,您需要一个简单的 HTML 网站,具有常规的形式。
下一个增强是 CSS - 你让它看起来不错。
然后您可以使用 Javascript 进一步增强它 - 您可以添加或删除元素、添加效果等。

对于旧浏览器(或者例如带有脚本拦截器的浏览器)来说,基本的 HTML 始终存在。

例如,发布评论的表单可能如下所示:

<form action="post-comment.php" method="post" id="myForm">
<input type="text" name="comment">
</form>

然后您可以使用 javascript 增强它,使其成为 AJAXy

$('#myForm').submit(...);

理想情况下,AJAX 回调应使用与 post-comment.php 相同的代码 - 通过调用同一文件或通过 include,那么你就不必重复代码。

The usual method is what's called progressive enhancement.

Basically you take a simple HTML website, with regular forms.
The next enhancement is CSS - you make it look good.
Then you can enhance it further with Javascript - you can add or remove elements, add effects and so on.

The basic HTML is always there for old browsers (or those with script blockers, for example).

For example a form to post a comment might look like this:

<form action="post-comment.php" method="post" id="myForm">
<input type="text" name="comment">
</form>

Then you can enhance it with javascript to make it AJAXy

$('#myForm').submit(...);

Ideally the AJAX callback should use the same code as post-comment.php - either by calling the same file or via include, then you don't have to duplicate code.

川水往事 2024-08-08 12:58:05

处理这个问题的一种方法是:

  • 首先,创建没有任何 javascript 的网站
  • 然后,当每项工作正常时,在合适的地方添加 javascript 增强功能

这样,如果禁用 JS,网站的“第一个”版本仍然可以工作。

当然,你也可以对 CSS 做同样的事情——甚至每天都有一个“CSS Naked Day” ,显示没有 CSS 的网站是什么样子 ^^

一个例子?

  • 您有一个标准的 HTML 表单,在提交时将数据 POST 到您的服务器,服务器重新创建页面时会显示一条消息,例如“感谢订阅”,
  • 然后您添加一些 JS + Ajax 内容:而不是重新加载整个内容提交表单时,您执行一个 Ajax 请求,仅发送数据; 作为回报,它会显示“感谢订阅”,而无需重新加载页面。

在这种情况下,如果禁用了 javascript,第一种“标准”的处理方式仍然有效。

这就是所谓的渐进增强(部分)

One way to deal with this is to :

  • First, create the site, without any javascript
  • Then, when every works, add javascript enhancements where suitable

This way, if JS is disabled, the "first" version of the site still works.

You can do exactly the same with CSS, naturally -- there is even one "CSS Naked Day" each day, showing what websites look like without CSS ^^

One example ?

  • You have a standard HTML form, that POSTs data to your server when submitted, and the re-creation of the page by the server displays a message like "thanks for subscriving"
  • You then add some JS + Ajax stuff : instead of reloading the whole page while submitting the form, you do an Ajax request, that only send the data ; and, in return, it displays "thanks for subscribing" without reloading the page

In this case, if javascript is disabled, the first "standard" way of doing things still works.

This is (part of) what is called Progressive enhancement

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