简体   繁体   English

加载外部CSS和JavaScript文件的顺序

[英]Order of loading external CSS and JavaScript files

I have a third party application that loads many css and javascript files, and I now want to optimize this by concatinating all the javascripts into one file, compressed by the yuicompressor, but ... when we have a mix like: 我有一个加载许多 css和javascript文件的第三方应用程序,我现在想通过将所有javascripts合并到一个文件中来优化它,由yuicompressor压缩,但是...当我们有一个混合时:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<link rel="stylesheet" href="style1.css" type="text/css" />
<script type="text/javascript" src="script3.js"></script>
<script type="text/javascript" src="script4.js"></script>

Does it matter that there's a css in the middle here? 这里中间有一个css是否重要? Should I concatinate and yuicompress the 4 javascripts and load them before the CSS or after? 我应该整理和yuic压缩4个javascripts并在CSS之前或之后加载它们吗?

Check out Yahoo's Best Practices for Speeding Up Your Web Site , they recommend loading your css first (preferably in the header), and your js last (after all the content in the body). 查看雅虎加速网站的最佳实践 ,他们建议首先加载你的css(最好是在标题中),然后你的js加载(在正文中的所有内容之后)。 Google's best practices also recommended loading CSS first. Google的最佳做法还建议首先加载CSS。

It depends. 这取决于。 If all the JS only works on DOM ready, the order is unimportant. 如果所有JS只能在DOM上运行,那么顺序就不重要了。 However, if there is any in-line javascript that changes CSS styling of DOM elements, then you'll have issues. 但是,如果有任何内联javascript更改DOM元素的CSS样式,那么您将遇到问题。

More practically, you should probably put the CSS first so there is less time where the user needs to experience unstyled content. 更实际的是,您可能应该首先使用CSS,以便用户需要更少的时间体验无格式内容。

It doesn't matter, although if loading takes a while, the user might see his page change appearance and wonder why. 这没关系,虽然如果加载需要一段时间,用户可能会看到他的页面改变外观并想知道为什么。 I'd put the CSS files first to have the style definitions in place before any DOM manipulation, most likely minimizing the visible change as the page loads, but it doesn't really matter in the end. 在任何DOM操作之前,我首先将CSS文件放在样式定义中,最有可能在页面加载时最小化可见变化,但最终并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM