在身体标记结束之前放置jQuery / javascript源页面 [英] Putting jQuery/javascript source pages before end of body tag

查看:47
本文介绍了在身体标记结束之前放置jQuery / javascript源页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个插件说明中看到,在body标记结束之前粘贴javascript / jQuery源代码。我做了搜索,为什么他们这样说,没有任何意义。

I've seen in several plugin instructions , paste the javascript/jQuery source just before the end of body tag. I made search why they are saying like that, didn't make me any sense.

如果我将src文件放在脚本中的任何地方,我根本不会遇到任何问题。有人能给我一个很好的答案吗?

If I put the src file where ever in the script, I never faced a problem at all. Could anyone give me a good answer about this?

推荐答案

如果javascript代码没有引用DOM或DOM中的任何对象,然后它可以放在你页面的任何地方。

If javascript code does not reference the DOM or any objects in the DOM, then it can be put anywhere in your page.

如果你把它放在< / body>之前的body标签中的HTML之后标记,然后在脚本加载之前解析并显示页面,这样可以更快地显示页面。因此,您看到的建议是最大化页面的初始显示性能。

If you put it AFTER the HTML in the body tag right before the </body> tag, then the page will be parsed and displayed before your scripts load which will get your page displayed faster. So, the recommendation you've seen is to maximize the initial display performance of your pages.

如果javascript引用DOM或DOM中的任何对象,那么它必须要么在使用jQuery中的 $(document).ready(fn)之类的内容之前等待DOM加载的特殊代码,要么必须在DOM,以便在加载DOM之前不会执行。

If javascript DOES reference the DOM or any objects in the DOM, then it must either have special code to wait for the DOM to be loaded before executing using something like $(document).ready(fn) in jQuery or the code must physically be loaded after the DOM so that it won't execute until the DOM is loaded.

当然,代码必须在它初始执行的任何代码立即依赖之后加载。因此,需要在jQuery库本身之后加载jQuery插件。

And, of course, code must be loaded after any code that it's initial execution immediately depends upon. So, a jQuery plugin would need to be loaded after the jQuery library itself.

这是一套通用指南:


  1. 在页面中尽可能晚地放置代码,以最大限度地提高页面的显示性能。

  2. 将代码置于其初始执行所依赖的任何其他库之后

  3. 只有在加载文档之前需要执行或使用该代码时,才将代码放入< head> 部分。例如,如果您的代码正在检查URL和cookie并决定是否进行客户端重定向,那么您希望该代码立即执行,这样您就可以将该代码放在< head> ; 部分,以便它可以在DOM加载或显示之前执行。作为另一个例子,如果你有一些内联javascript需要某些功能在页面加载期间可用(例如一些内联javascript,它执行 document.write()并调用一些实用程序函数),然后将这些实用程序函数放在< head> 部分中,以便在页面加载时可用。

  4. 如果有没有理由在页面加载之前执行代码,或者代码需要访问DOM本身,然后将代码放在< / body> 标记之前以进行优化页面显示代码运行时DOM准备好操作的代码的时间和位置。

  5. 尽可能将代码放在外部JS文件中,以最大限度地利用浏览缓存。

  1. Put code as late as possible in the page to maximize the display performance of your page.
  2. Put code after any other libraries that its initial execution depends on.
  3. Put code in the <head> section only if that code needs to execute or be used before the document loads. As an example, if you had code that was examining the URL and cookie and deciding whether to do a client-side redirect, you want that code to execute immediately so you might put that code in the <head> section so it can execute before the DOM loads or displays. As another example, if you have some inline javascript that needs certain functions to be available during the page load (e.g. some inline javascript that does document.write() and calls some utility functions), then put those utility functions in the <head> section so they are available as the page loads.
  4. If there is no reason to execute the code before the page loads or if the code needs to access the DOM itself, then put the code right before the </body> tag to optimize page display time and position the code where the DOM is ready for manipulation when the code runs.
  5. Put code in external JS files whenever possible to take maximum advantage of browsing caching.

这篇关于在身体标记结束之前放置jQuery / javascript源页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆