请求 .js 文件的最简单的 javascript 函数是什么?

发布于 2024-09-16 11:01:31 字数 229 浏览 2 评论 0原文

我只是想知道请求服务器端 .js 文件的最简单的 javascript 函数是什么。目前我有一个 jquery-1.4.2.min 文件,大小为 70kb,我认为必须有一种方法,使用 javascript 来请求该文件。这样,如果用户没有启用 javascript,该函数将被忽略,并且不必下载 jquery 文件,从而加快页面的下载速度并减少服务器使用的带宽。

另外,如果这有效,文件会被下载,还是页面会开始使用它?提前致谢!

I was just wondering what the simplest javascript function would be to request a server side .js file. Currently I have a jquery-1.4.2.min file that weighs in at 70kb, and I figured that there has to be a way, using javascript, to request this file. That way, if the user doesn't have javascript enabled the function would be ignored and the jquery file wouldn't have to be downloaded, thus speeding up the download of the page and decreasing the bandwidth used by the server.

Also if this works, would the file just be downloaded, or would the page begin to use it? Thanks in advance!

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

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

发布评论

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

评论(3

2024-09-23 11:01:31

大多数浏览器在 JavaScript 被禁用时已经不会下载它,因此这对于大多数浏览器用户来说是过度优化的。如果我能找到关于它的问题,我会更新这个......但这是你不需要处理的事情:)

编辑: 这是这个问题,尽管我认为还有另一个类似的问题。

另外需要记住的是,用户只会下载一次 如果您的缓存标头设置正确。另请查看为 jQuery include 使用 CDN

Most browsers already don't download JavaScript when it's disabled, so this is an over-optimization for most browser users. If I can find the question on it I'll update this...but it's something you don't need to handle :)

Edit: Here's that question, though I think there's another similar one as well.

Something else to keep in mind is that the user will only download it once if your cache headers are set correctly. Also take a look at using a CDN for your jQuery include.

财迷小姐 2024-09-23 11:01:31

如果用户未启用 javascript,则具有 src 属性的

If the user doesn't have javascript enabled, <script> elements with src attributes will be ignored.

恏ㄋ傷疤忘ㄋ疼 2024-09-23 11:01:31

如果您确实愿意,可以 document.write() 脚本标记或创建脚本元素并附加它。如果 js 被禁用,则永远不会发生这种情况。但其他人已经提到,对于大多数现代浏览器,如果禁用 js,脚本标签将被忽略,所以这是矫枉过正。

If you really want to, you can document.write() the script tag or create a script element and append it. If js is disabled it will never happen. But others have mentioned already, for most modern browsers, the script tag will simply be ignored if js is disabled, so it's overkill.

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