页面加载完成时加载并运行js代码(不引人注目的方式)
我想尽可能地将 JS 代码与 HTML 分开,我可以看到几种模式。
使用
$(document).ready(function() {...})
1) 我可以在关闭 body 标签之前
2) 我可以在关闭 body 标签之前将 js 代码放入
new FormValidationHandler()
脚本标签中
3) 我可以在脚本标签中指向包含 $(document)ready 或 new FormValidationHandler 等初始化的外部 js 文件
4 )还有一种使用自调用函数的方法,但不知道它是否映射到这个问题
我的问题是哪种方法是首选?
第二个是,我可以在两个地方将外部脚本放入网页中:
- 在 body 标记中的head 标记中
- (通常在末尾)
head 应该只包含不必在页面加载时运行的代码吗?那么该代码应该放在body中吗?
I'd like to separate my JS code from HTML as much as possible and I can see several patterns for that.
1) I can use
$(document).ready(function() {...})
just before closing body tag
2) I can just put js code like
new FormValidationHandler()
in script tag just before closing body tag
3) I can point external js file containing initialization like $(document)ready or new FormValidationHandler in script tag
4) there is also a way to use self-invoking function but don't know if it maps to this problem
My question is which way is preferred?
Second one is that there are two places I can put my external scripts into the web page:
- in the head tag
- in the body tag (usually at the end)
Should head contain only code that doesn't have to run on page load? Then that code should be placed in body?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我喜欢做的事情。它可能并不完美,但我喜欢这样:
HTML 文档中的脚本位置:
每个脚本都在 HTML 文档末尾加载,就在结束正文之前。
有一个例外:处理 FOUC 的脚本(例如modernizr)。这个脚本必须在头部。
我没有看到任何其他合理的例外。
脚本组织:
在我看来,有两种情况:如果您使用超文本文档或网络应用程序(也许这可能需要更多解释,但会很长:p)。我很少从事网络应用程序的工作,所以我还没有一个经过验证的组织来做这件事。但我认为在网络应用程序中,您可能可以使用一些脚本加载库,例如 requirejs ,它可能比简单的网页。
对于超文本文档(大多数网页),我喜欢区分两种脚本:库和我在法语中所说的“script d'interfaçage”(“链接脚本”可能是一个很好的翻译......)。
顾名思义,库是在 javascript 环境中加载库的脚本,但不执行任何操作。
链接脚本用于将这些库链接到特定的 HTML 文档。
对我来说,在一个完美的世界中,应该有与您一样多的库脚本,但每个 HTML 文档只有一个链接脚本。如果您使用 jQuery,则在此脚本中您将找到 $(document).ready 调用,并且该脚本的所有内容都应该非常非常简单。理想情况下,在文档就绪函数中应该只有如下指令:
这种类型的指令实际上是 HTML 文档和 JS 库之间的简单链接,并且非常易于阅读和理解。
通过这个组织,你可以进行任何类型的打包来减少要加载的js文件的数量。该打包必须针对客户端缓存和限制 HTTP 请求等进行优化...
外部文件还是内联脚本?
就我个人而言,我更喜欢对所有脚本使用外部文件,但通常我使用一个内联脚本标签来声明某些库所需的一些变量(广告服务的密钥等......)。
加载外部库
最后一个具体情况:当您必须从另一台主机加载脚本时。一般来说,你无法判断脚本是否会加载,因为你无法判断其他服务器是否已启动,以及它会慢还是快......你无法准确判断这个脚本的内容会这样做,所以它可能会破坏你的页面...
从其他主机加载脚本确实会产生问题,这就是为什么我建议在页面完全加载后异步加载它们,并使用尽可能多的控件。
为此,我亲自开发了一个专门用于加载库的库(也许有一天我会在有时间的时候将其发布到 gitHub 上)。
例如,我使用此脚本加载 Facebook google+ ou twitter API,或任何其他外部库,例如统计计数器或广告服务。
there is how I like to do. It's probably not perfect, but I like it this way :
Script location in the HTML document :
Every script loaded at the end of the HTML doc, just before the closing body.
There's one exception : the script that handles the FOUC (modernizr for example). This script must be in the head.
I don't see any other reasonable exceptions.
Scripts organization :
Their's two cases for this, in my opinion : If you work with an Hypertext document, or a web app (Maybe this could need some more explanations, but it would be long :p ). I rarely worked for web apps, so I don't yet have a validated organization for this. But I think in a web app you can probably use some script loading libraries like requirejs and it will probably be more useful than for simple web pages.
For a Hypertext document (most web pages), I like to distinguish two kind of scripts : libraries and what I call in french "script d'interfaçage" ("linking script" could probably be a good translation...).
Libraries are, as the name says, scripts that loads libraries in the javascript environment, but doesn't DO anything.
linking script is made to link those libraries to the specific HTML doc.
For me, in a perfect world, there should be as much library scripts as you which but only one linking script for each HTML doc. In this script you'll find the $(document).ready call if you're using jQuery, and all the content of this script should be very very very simple. Ideally there should be, in the document ready function, only instructions like :
This type of instruction is really a simple link between the HTML doc and the JS library, and it's very simple to read and understand.
Over this organization, you can than do any kind of packaging to reduce the number of js files to be loaded. This packaging have to be optimized for client caching and limiting HTTP requests etc...
External files or inline scripts ?
Personally, I prefer to use external files for all scripts, but generally I use one inline script tag to declare some variables needed for some libraries (your key for your ad service etc...).
Loading of external libraries
One last specific case : When you have to load a script from another host. Generally, you can't tell if the script will load or not, because you can't tell if the other server is up or not, and if it will be slow or fast... You can't tell exactly what this script will do, so it could break your page...
Loading scripts from other hosts can really create problems, and this is why I recommend loading them asynchronously, once your page is fully loaded, with as much controls as you can.
To do this, I personally developed a library dedicated to loading libraries (maybe one day I'll publish it on gitHub, when I have some time).
For example, I use this script to load Facebook google+ ou twitter APIs, or any other external libs like stat counter or ads services.