禁用 javascript 时如何不加载 javascript 文件

发布于 2024-10-13 16:04:02 字数 2519 浏览 6 评论 0原文

[已解决]

我通常在没有 javascript 的情况下构建页面,然后才添加 javascipt 功能。这样,我可以使用需要 javascript 的工具来设置所有 div 并显示:none,然后在 javascript 的第一行中将其变为可见:

<script type="text/javascript">
    $(document).ready(function(){
        $('#jsdivs').show();
    });
</script>

这样做,我就可以摆脱那些因任何原因禁用浏览器 javascript 的人。

遵循这种精神,我想找到一种默认情况下不加载任何 javascript 的方法,然后,如果启用了 javascript,则有加载 javascript 文件的说明。

现在我正在像这样加载 JQuery(出于缓存原因,很多人可能已经缓存了这个):

<script src="http://code.jquery.com/jquery-1.4.4.js"></script>

另一个文件最小化了所有其他 javascript。一切都在最后,就在 HTML body 标记之前。

我在此处这里但没有一个可以帮助我解决这个问题。首先,我想知道这是否是完成此任务的好方法。多谢。

更新:

  • @Lèse majesté 向我指出了一个旧的讨论,说禁用时浏览器不会加载 javascript 文件。由于我认为这些信息不够确凿,我做了一些测试,通过插件“Web Developer”在 Firefox 中禁用 javascript,YSlow(Firebug 的另一个 FF 插件)继续显示所有 javascript 信息,更不用说“Live Headers”(另一个出色的 FF 插件,可以实时显示标头)还显示请求中存在 _utmc cookie(通过 Google Analytics 脚本)。

  • @Marcel Korpel 发表了非常好的评论(尽管显然很势利),这为真正的解决方案开辟了道路。他说,Google Analytics 使用“动态包含 ga.js 的方法。需要结束标记(以及立即开始标记)来让浏览器更新 DOM 树并实际加载和执行添加的脚本。”

  • 有关的信息指示 我可以想出这个 PHP 代码(我必须将 PHP 代码放入Pastie.org 因为 Stackoverflow 搞乱了它):

    http://pastie.org/1487432

打印:

<script language="javascript" type="text/javascript">
    document.write('\<script src\="js\/minified\.js" type\="text\/javascript"\>\<\/script\>');
</script>

这次 YSlow 和 Live Headers 没有提及任何 javascript 文件。

[SOLVED]

I use to build pages normally without javascript and only then to add javascipt features. This way, I can set all divs with tools that require javascript with display:none and then in the first line on javascript to turn it visible:

<script type="text/javascript">
    $(document).ready(function(){
        $('#jsdivs').show();
    });
</script>

Doing so I get out of the way of people who for any reason have their browsers javascript disabled.

Following this spirit, I'd like to find a way to not load any javascript by default and then, if javascript is enabled have instructions to load the javascript files.

Right now I am loading JQuery like this (for cache reasons, many people may already have this cached):

<script src="http://code.jquery.com/jquery-1.4.4.js"></script>

And another file with all the other javascript minimized. All in the end, just before the HTML body tag.

I saw many opinions here and here but none that help me with this. At first, I'd like to know if that's a good way of getting this done. Thanks a lot.

UPDATE:

  • @Lèse majesté indicated me an old discussion saying that browsers do not load javascript files when it is disabled. As I didn't think the information was enough conclusive, I made some tests disabling javascript in Firefox via the plugin "Web Developer" and YSlow (another FF plugin for Firebug) keeps on showing all javascript information, not mentioning that "Live Headers" (another fantastic FF plugin that shows the headers in realtime) also showed that a _utmc cookie (by the Google Analytics script) was present in the request.

  • @Marcel Korpel made a very good (though apparentely snob) comment, which opened the way for a real solution. he said that Google Analytics uses a "method to dynamically include ga.js. The closing tag (and immediately opening tag) is needed to let the browser update the DOM tree and actually load and execute the added script."

  • With the information on his indication I could come up with this PHP code (I had to put the PHP code in Pastie.org because Stackoverflow was messing with it):

    http://pastie.org/1487432

That prints:

<script language="javascript" type="text/javascript">
    document.write('\<script src\="js\/minified\.js" type\="text\/javascript"\>\<\/script\>');
</script>

This time YSlow and Live Headers say nothing about any javascript file.

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

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

发布评论

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

评论(2

瀟灑尐姊 2024-10-20 16:04:02

如果禁用 JavaScript,大多数浏览器不会下载链接的 .js 文件。请参阅此问题:

浏览器是否下载如果用户禁用了 JS,那么会生成 JS 文件吗?

Most browsers don't download linked .js files if JavaScript is disabled. See this question:

Does a browser download JS files if the user has JS disabled?

-柠檬树下少年和吉他 2024-10-20 16:04:02

您可以动态添加 javascript 文件,但内容不会被评估并且无法供您使用(因为 javascript 已加载并解析一次),因此此方法将不起作用,如 此处

如果您能够通过查询字符串参数、表单字段或任何您喜欢的内容将此信息(禁用 JavaScript)传递回服务器,您还可以使用服务器端脚本并避免写出脚本标记。

但是,请考虑所涉及的复杂性以及这是否可以为您节省任何东西。

说实话,如果 javascript 关闭,我不知道浏览器对这些文件的行为如何 - 有理由假设(尽管应该始终检查)如果 javascript 关闭,也不会请求任何 javascript 文件。< /strike>

更新:

可以看出 此处,如果禁用 JavaScript,大多数浏览器不会下载 js 文件。

You can add javascript files dynamically, but the contents will not be evaluated and not be available to you (since javascript is loaded and parsed once), so this approach will not work as can be seen here.

You can also use server side scripting and avoid writing out script tags if you are able to pass this information (javascript disabled) back to the server on a query string parameter, form field or whatever you like.

However, consider the complexity involved and whether this saves you anything.

To be honest, I don't know how browsers behave regarding these files if javascript is off - it is reasonable to assume (though one should always check) that if javascript is off, no javascript files would be requested either.

Update:

As can be seen here, most browsers do not dowload js files if javascript is disabled.

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