js和css写在同一个文件里
2013-03-13 11:28 youxin 阅读(971) 评论(0) 编辑 收藏 举报Now, if you have by any chance worked on page load optimizations, you would know how costly each resource download is. The more the number of external resources that you refer to, the more the time it takes for the page load to complete.
Typically, web pages refer to many external CSS and JS files and hence incur many resource downloads. The advice from the PLT (page load time) gurus is to combine all the CSS files into one and all the JS files into one so as to reduce the number of resource downloads to just two. This, without any doubt, would help boost your PLT.
If you feel that two downloads still isn't the best, I concur. In this post, we'll look at a technique to combine CSS with JS and get the resource download count to one! I discovered this technique while desperately trying to improve the page load time for the pages in Microsoft Office Live. Read on...
The technique relies on how CSS and JS parser behaves in IE and Firefox.
- When the CSS parser encounters a HTML comment token (<!--) in CSS content, the token gets ignored.
- When the JS parser encounters a HTML comment token (<!--) in JS content, the token is treated similar to the line comment (//) and hence the rest of the line following the HTML comment token gets ignored
Look at the below JS+CSS code snippet...
<!-- /*
function t(){}
<!-- */
<!-- body { background-color: Aqua; }
When the CSS parser parses the above content, the HTML comment tokens would be dropped and the snippet would become equivalent to the one below...
/*
function t(){}
*/
body { background-color: Aqua; }
As you can see above, the CSS parser will get to see only the CSS code and the script code would appear to be within the comments (/* ... */).
In similar lines, when the JS parser parses the content, the HTML comment tokens would be treated as line comments (//) and hence the code snippet would become equivalent to the one below...
// /*
function t(){}
// */
// body { background-color: Aqua; }
As you can see above, the JS parser will get to see only the script code and the rest of the contents would look like comments.
You can refer to the above content in both the SCRIPT and LINK tags in your HTML page. For e.g.,
<link type="text/css" rel="stylesheet" href="test.jscss" />
<script type="text/javascript" language="javascript" src="test.jscss"></script>
Note above that both the tags refer to the same source and hence it would be downloaded once and used as appropriate (as CSS and SCRIPT).
To round it off, there is just one more thing that you need to take care of - the response content type - it needs to be set to */* in order to affirm to Firefox that the contents can be treated as anything as appropriate.
I've attached a sample project (created with VS 2005 SP1) that demonstrates this technique. Enjoy!
Update: (put in place due to popular misconception) In the actual implementation you will have the JS and CSS files separately on disk. At runtime, you'll have to combine them by adding the HTML comments appropriately and serve with the correct content type and cache headers. You should also remember the dynamically built content so that you don't need to rebuild them for each request. Please check out the attached sample app. This will ensure that you don't compromise on the readability, maintainability etc. while at the same time taking advantage of the reduced number of network calls.
Note(s):
- If the JS contains any multi line comments it should be removed before combining with the CSS using this technique.
- This technique has been tested positive on IE6, IE7 and Firefox 2.0. It may work on other browsers off the shelf or may need minor tweaks.
- 下载测试文件:http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-02-35-62-78/JsCssUpdated.zip
via:http://blogs.msdn.com/b/shivap/archive/2007/05/01/combine-css-with-js-and-make-it-into-a-single-download.aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!