data URI scheme

发布: 2010-04-29 19:11

http://en.wikipedia.org/wiki/Data_URI_scheme


data URI scheme
From Wikipedia, the free encyclopedia
The data URI scheme is a URI scheme that provides a way to include data in line in web pages as if they were external resources. It tends to be simpler than other inclusion methods, such as MIME with cid or mid URIs. Data URIs are sometimes called Uniform Resource Locators, although they do not actually locate anything remote. The data URI scheme is defined in RFC 2397 of the Internet Engineering Task Force (IETF).
Although the IETF published the data URI specification in 1998,[1] they never formally adopted it as a standard.[2] But the HTML 4.01 specification refers to the data URI scheme[3], and data URIs have now been implemented in most browsers.
Contents [hide]
1 Web browser support
2 Advantages
3 Disadvantages
4 Format
5 Examples
5.1 HTML
5.2 CSS
5.3 JavaScript
5.4 Inclusion in HTML or CSS using PHP
6 Conversion tools
7 See also
8 References
9 External links
[edit]Web browser support

Data URIs are currently supported by the following web browsers:
Gecko-based, such as Firefox, XeroBank, Camino, Fennec and K-Meleon
Konqueror, via KDE's KIO slaves input/output system
Opera
WebKit-based, such as Safari (including on iPhones), Android's browser, Epiphany and Midori (WebKit is a derivative of Konqueror's KHTML engine, but Mac OS X does not share the KIO architecture so the implementations are different)
Chromium-based, such as Chrome and Iron
Internet Explorer 8: Microsoft has limited its support to certain "non-navigable" content for security reasons, including concerns that JavaScript embedded in a data URI may not be interpretable by script filters such as those used by web-based email clients. Data URIs must be smaller than 32 KiB.
"Data URIs are supported only for the following elements and/or attributes:
object (images only)
img
input type=image
link
CSS declarations that accept a URL, such as background, backgroundImage, and so on." -MSDN[4]
TheWorld Browser, an IE shell browser which has a built-in support for Data URI scheme
[edit]Advantages

HTTP request and header traffic is not required for embedded data, so data URIs consume less bandwidth whenever the overhead of encoding the inline content as a data URI is smaller than the HTTP overhead. For example, the required base64 encoding for an image 600 bytes long would be 800 bytes, so if an HTTP request required more than 200 bytes of overhead, the data URI would be more efficient.
For transferring many small files (less than a few kilobytes each), this can be faster. TCP transfers tend to start slowly. If each file requires a new TCP connection, the transfer speed is limited by the round-trip time rather than the available bandwidth. Using HTTP keep-alive improves the situation, but may not entirely alleviate the bottleneck.
When browsing a secure HTTPS web site, web browsers commonly require that all elements of a web page be downloaded over secure connections, or the user will be notified of reduced security due to a mixture of secure and insecure elements. HTTPS requests have significant overhead over common HTTP requests, so embedding data in data URIs may improve speed in this case.
Web browsers are usually configured to make only a certain number (often two) of concurrent HTTP connections to a domain,[5] so inline data frees up a download connection for other content.
Environments with limited or restricted access to external resources may embed content when it is disallowed or impractical to reference it externally. For example, an advanced HTML editing field could accept a pasted or inserted image and convert it to a data URI to hide the complexity of external resources from the user.
It is possible to manage a multimedia page as a single file.
[edit]Disadvantages

Data URIs are not separately cached from their containing documents (e.g. CSS or HTML files) so data is downloaded every time the containing documents are redownloaded.
Content must be re-encoded and re-embedded every time a change is made.
Internet Explorer through version 7 (approximately 35% of the market as of February 2010), lacks support.
Internet Explorer 8 limits data URIs to a maximum length of 32 KB.[4]
Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.
Base64-encoded data URIs are 1/3 larger in size than their binary equivalent. This overhead is reduced to 2-3% if the HTTP server compresses the response using HTTP's Content-Encoding header.
Data URIs make it more difficult for security software to filter content.[6]
[edit]Format

data:[][;charset=""][;base64],
The encoding is indicated by ;base64. If it's present the data is encoded as base64. Without it the data (as a sequence of octets) is represented using ASCII encoding for octets inside the range of safe URL characters and using the standard %xx hex encoding of URLs for octets outside that range. If is omitted, it defaults to text/plain;charset=US-ASCII. (As a shorthand, the type can be omitted but the charset parameter supplied.)
Some browsers are strict in the ordering if both ;base64 and ;charset are supplied. Google Chrome, for instance, only works correctly if ;base64 appears before ;charset, e.g. data:text/html;base64;charset=utf-8,data.
[edit]Examples

[edit]HTML
An HTML fragment embedding a picture of a small red dot:
Red dot
As demonstrated above, data URIs may contain whitespace for readability.
[edit]CSS
A CSS rule that includes a background image: (in Mozilla Firefox 3.5.7 encoded data must not contain new lines)
ul.checklist li.complete { margin-left: 20px; background:
url('data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/
//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U
g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC')
top left no-repeat; }
[edit]JavaScript
A JavaScript statement that opens an embedded subwindow, as for a footnote link:
window.open('data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Cht'+
'ml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2F'+
'title%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0'+
'A%3C%2Fhtml%3E%0A%0D%0A','_blank','height=300,width=400');
This example does not work with Internet Explorer 8 due to its security restrictions that prevent navigable file types from being used.[4]
[edit]Inclusion in HTML or CSS using PHP
Because base64-encoded data URIs are not human readable, a website author might prefer the encoded data be included in the page via a scripting language such as PHP. This has the advantage that if the included file changes, no modifications need to be made to the HTML file, and also of keeping a separation between binary data and text based formats. Disadvantages include greater server CPU use unless a server-side cache is used.
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
?>

An elephant
Similarly, if CSS is processed by PHP, the above function may also be used:


div.menu
{
background-image:url('');
}
In either case, client or server side features (such as dynamic content generation), client detection and awareness (to support selection of alternative content, such as a different language), or discrimination (content filtering based on some client deficiency) systems (like conditional comments) may be used to provide a standard http: URL for Internet Explorer and other older browsers.
[edit]Conversion tools

Clipboard Observer is a free java tool for easy converting PNG files to URI scheme
The data: URI kitchen is a web-based conversion utility for converting text or arbitrary files to data URIs.
Base64 Encoder, a Firefox Extension, converts PNG, JPEG, GIF, BMP and ICO images to base64 data URIs (into the browser's location bar, to the clipboard or to file), plainly or in pre-formatted styles for HTML. The current version (1.3.2 as at early 2010) handles ICO files as 'image/x-icon' and is expected to be upgraded for use in Firefox 3.6 when this is fixed.
SingleFile, a Google Chrome extension, embeds all images, css and scripts into the current page in order to save it into one single HTML file (WebArchive/MHTML-like).
OpenSSL will encode/decode Base64. To decode from Base64: openssl base64 -d -in -out , and to encode to Base64: openssl base64 -in -out
[edit]See also

An alternative for attaching resources to an HTML document is MHTML, usually found in HTML email messages.
MIME for the used mediatypes.
The data URI scheme is tested in the Acid2 and Acid3 tests.
[edit]References

^ Masinter, L (August 1998). "RFC 2397 - The "data" URL scheme". Internet Engineering Task Force. Retrieved 2008-08-12.
^ "Proposed Standards". Official Internet Protocol Standards. Internet Society. 2009-01-04. Retrieved 2009-01-04.
^ Raggett, Dave; Le Hors, Arnaud; Jacobs, Ian (1999-12-24). "Objects, Images, and Applets: Rules for rendering objects". HTML 4.01 Specification. World Wide Web Consortium. Retrieved 2008-03-20.
^ a b c "data Protocol". Microsoft Developer Network. Microsoft. Retrieved 2009-01-05.
^ "RFC 2616 Section 8". World Wide Web Consortium.
^ Masinter, L (August 1998). "Security". RFC 2397 - The "data" URL scheme. Internet Engineering Task Force. pp. 2. Retrieved 2008-08-12.
[edit]External links

RFC 2397
About data: URLs and the Mozilla implementation
data: URL tests
Using Data URLs effectively with Cascading Style Sheets


原文: http://qtchina.tk/?q=node/432

Powered by zexport