- 浏览: 229280 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
jj7jj7jj:
容我来补一刀~1,function(){ ...
执行JS匿名函数的N种方式 -
duwu:
根本就不能用,试了好多天,一次都没发送成功,发了根本就没有任何 ...
邮件发新浪微博 -
ganky:
终于搞定了,我郁闷啊……我是用JAVA写的,在登录成功后使用g ...
基于web飞信接口的飞信应答机器人 -
ganky:
之前也有搞了一下,只实现了登录,因为一直找不到webim_se ...
基于web飞信接口的飞信应答机器人 -
kewin:
现在127行了哦
基于web飞信接口的飞信应答机器人
为了进一步减少HTTP请求数量,提高web应用性能,可以把分别压缩合并后的JS和CSS再次合并到一个文件中:http://blogs.msdn.com/shivap/archive/2007/05/01/combine-css-with-js-and-make-it-into-a-single-download.aspx。想法不错。我猜想利用IE css中的expression把js直接嵌入到css中,也可以实现这个效果。
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.
引用
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.
发表评论
-
Net::OICQ 还可以登录qq
2010-01-12 21:17 1225perl -MCPAN -e shell CPAN& ... -
使用chrome浏览器开发如何清除dns缓存
2009-12-30 11:26 5381使用firefox开发太占资源,使用chrome开发速度很快, ... -
Opera Unite可以不经代理,真正P2P直连
2009-11-24 09:47 1417Opera Unite allows you to share ... -
XSS-跨站攻击
2009-09-21 14:19 870最全的XSS跨站攻击方式:http://ha.ckers.or ... -
MSN批量添加联系人
2009-09-18 13:34 1647MSN的联系人导出格式为后缀为.ctt,是xml。创建一个符合 ... -
ThunderBird支持导入csv格式通讯录了
2009-09-17 16:56 25412.0.0.23版本支持从文本文件导入。原本还想写个插件哪,没 ... -
-moz-zoom-in 和-moz-zoom-out
2009-09-16 08:59 1269/*光标样式--自定义图片,多个备选光标样式可以" ... -
SVN--POST_COMMIT hook
2009-08-17 19:18 1269@echo off set TEMPDIR=C:\tem ... -
使用thunderbird命令行发邮件
2009-08-17 19:14 2599thunderbird -compose "to ... -
强大的网络协议分析工具--Wireshark
2009-07-28 11:25 1337http://www.wireshark.org/ 引用 D ... -
廉价的nobr标签
2009-06-26 21:30 1015对于较长的字符不折行显示,除了使用css,<nobr&g ... -
PPK的innerHTML性能测试
2009-06-22 12:44 888http://www.quirksmode.org/dom/i ... -
动态解析dojoType声明的组件
2009-06-21 15:31 1531//比如动态解析服务器端应答的dijit标签(虽然这不是推 ... -
两道Javascript面试题
2009-06-21 13:48 1514//实现点击第i个div时仅alert该div对应的i值; ... -
定制IE下载对话框的按钮
2009-06-21 10:42 1052http://blog.csdn.net/WinGeek/ar ... -
Opera Dragonfly
2009-06-17 23:56 826Opera Dragonfly很强大,使用上还没有firebu ... -
firebug + fiddle = ?
2009-06-17 23:47 825firebug中net面板中可以监控所有资源请求,因此理论上添 ... -
Opera Turbo--why it is faster?
2009-06-17 22:28 888引用 Opera Turbo runs on a person ... -
搜狗拼音是否可以开放webservice?
2009-06-17 22:25 1354翻译可以提供webservice(如google翻译和金山词霸 ... -
龙博js英文网站地址
2009-06-13 14:04 914龙博js英文网站地址
相关推荐
HTML5 Gamesshows you how to combine HTML5, CSS3 and JavaScript to make games for the web and mobiles - games that were previously only possible with plugin technologies like Flash. Using the latest ...
Starting with a little reference and a handful of lines of custom JavaScript, you will have a complete Single Page Application before you know it. What You’ll Learn Examine Vue.js templating ...
Starting with a little reference and a handful of lines of custom JavaScript, you will have a complete Single Page Application before you know it. What You’ll Learn Examine Vue.js templating ...
Combine Angular 2 and Bootstrap 4 along with Firebase in the development of a solid example Table of Contents Chapter 1. Saying Hello! Chapter 2. Working with Bootstrap Components Chapter 3. Advanced ...
Ensure seamless implementation of a JavaScript & HTML 5 CSS based frontend and PHP based backend. Learn about problem identification, best strategies, and UI design patterns as well to build a clean, ...
This book teaches you how to write declarative and reactive iOS applications using Apple's Combine framework. Combine uses a multitude of advanced Swift features such as generics, so you should have ...
The introduction of Combine into the Swift ecosystem now gives you a native way to manage asynchronous events in Swift, meaning you don’t have to rely on third-party reactive frameworks for event-...
Learning React Native - Building Native Mobile Apps with JavaScript 2nd ... Combine a large application’s many screens into a cohesive UX Handle state management in a large app with the Redux library
and on top of that, it will help you get a better understanding of the future of Node.js and how to make your own contributions a part of it. What this book covers Chapter 1, Welcome to the Node.js ...
Instead of manually combining meshes, which is very tedious, MCS will do this automatically for you and the performance improvements it gives cannot be achieved with manual combining. Just simple drag...
Hence, it becomes possible for the attacker to issue a command to all the nodes, that target a single node (for example, all nodes in the botnet might be commanded by the attacker to send a TCP SYN ...
Power Query allows you to easily discover, combine, and refine data from a variety of sources, so you can make accurate judgments with all the available information. Geographical awareness is another...
It follows a browser-neutral point of view so you can easily develop sites that are compatible with IE6, IE7, Firefox 2, Opera 9, and Safari 2.Packed with detailed examples in syntax-colored code and ...
此扩展名通过创建仅包含正在使用的选择器的新CSS样式表来删除页面上未使用CSS规则。 这不仅可以整理和优化样式表,而且可以将样式表合并为一个文件,然后可以下载该文件。 扩展使用的方法是基于消除所有ID和类的选择...
Have you ever needed to combine multiple files into a single destination (output) file? You can use a combination of a script and tool to create a single file from multiple files. Sum Column/Selection...
It breaks down tasks in game development into different roles, it includes: UI editor for UI graphic artists, Animation editor for graphic artists, Number cruncher for game data designers, Scene ...
Get a practical introduction to React Native, the JavaScript framework for writing and deploying fully featured mobile apps that render natively. The second edition of this hands-on guide shows you ...