2010-01-04
Data URI 和 URL
栏目:css+xhtml , javascript , web前端share
518 views
4 枚回复
DATA URI 可能会覆盖面越来越多,就目前来说,它是在优化http请求数目等方面最好的选择
首先这里的URI 不是URL,要区别一下:
URL 是(uniform resource locator) 的缩写。URL综合了协议和已存在资源的地址的两个元素。每一个公开访问的资源,包括图片,js文件,html文件和样式表都是有一个url告诉我们的浏览器从哪里去下载这些东西。
URI是(uniform resource identifier)的缩写。每一个URL其实也是URI,一个URI显示的除了一个获取信息的协议之外,还有关于该资源的附加信息,这些附加信息可能是地址信息(如果是地址信息那么就是URL),当然也可能是其它信息,当时其它信息的时候URI就不是URL,所以URI和URL不是一个东西。
DATA URI 可能会覆盖面越来越多,就目前来说,他是最适合在优化http请求数目的方面最好的选择
Data URI format
data URI 的格式简单漂亮,在 RFC 2397 (which actually is short enough that you can read it all)中描述的很详细。基本的格式如下 :
data:[<mime type>][;charset=<charset>][;base64],<encoded data>
下面来解释下上面代码:data:当然表明这是一个data URI 协议。第二部分:MIME type 表明这里的数据的表现形式。比如 png图,那应该就是image/png ,如果没有特别的指定,这里的MIME type的默认类型就是text/plain。第三部分character 可以设置,但是一般情况下来说可以省略,并且不适用于所有的图片。第四部分是就是设置编码格式,一般来说必须使用base 64来编码。如果数据不适用base 64来编码,那么数据就会使用url-encode来编码,比如像素就会编成%xx格式。
引用原文:
Performance implications
The most interesting part of the data URI story is that it gives you the ability to embed files inside of other files. Most writeups focus on embedding data URIs in CSS files as a way to improve performance. Indeed, there’s been a lot of research indicating that HTTP requests are one of the major performance holes for web sites, and decreasing the number of requests results in better page performance. “Minimize HTTP requests” is actually the first rule of the Yahoo! Exceptional Performance Best Practices, and it specifically mentions data URIs:
Inline images use the data: URI scheme to embed the image data in the actual page. This can increase the size of your HTML document. Combining inline images into your (cached) stylesheets is a way to reduce HTTP requests and avoid increasing the size of your pages. Inline images are not yet supported across all major browsers.
This is good advice for using data URIs: you want to use them where they will be cached most frequently. Regular images downloaded over HTTP are cached according to their headers and/or browser-specific settings so that they needn’t be re-downloaded all the time. Data URIs are considered to be part of the file that contains them, so they are part of the HTML or CSS file in which it is embedded. This means that the data URI has no standalone cache control policy. Embedding data URIs in your files make the files themselves larger, and if the file changes frequently (such as the homepage of a blog), then the larger file must be downloaded frequently. This slows down your site.
The easiest usage is to embed data URIs in external style sheets that are aggressively cached. That way, the empty cache experience is faster (due to fewer overall requests) and the primed cache experience is the same.
Browser support
Most modern browsers support data URIs:
- Firefox 2+
- Opera 7.2+ – data URIs must not be longer than 4100 characters
- Chrome (all versions)
- Safari (all versions)
- Internet Explorer 8+ – data URIs must be smaller than 32k
Since data URIs are not supported in IE prior to version 8, you need to decide whether or not it’s worthwhile to serve alternate content to those browsers (read Stoyan’s post).
Conclusion
Data URIs are an interesting and unique concept on the Web, and are likely to get more coverage going forward. For the time being, it seems that they are best suited to performance-related tasks, but who knows where the future will take us. In the short term, you can see some good performance savings by using data URIs to eliminate additional HTTP requests for fetching images. Data URIs also open up the possibility of generating images dynamically using JavaScript, though growing support for <canvas> may make this use case obsolete.
参考资料:http://www.nczonline.net/blog/2009/10/27/data-uris-explained/#comment-4298
Data URIs for CSS Images: More Tests, More Questions
相关文章介绍:Data URI 和 MHTML –from 秦歌
对于DataURI 和MHTML 解析很详细,感兴趣可以去view,别忘记留言
标签: Data URI , MHTML , URI , URL
转载注明:转自有米啦
本站遵循:署名-非商业性使用-禁止演绎 3.0 共享协议
收藏分享:
QQ书签 /
百度收藏 /
Google书签 /
收藏到鲜果 /
Digg /
Del.icio.us

