Home Web Front-end HTML Tutorial Data URI and MHTML completely solve all browsers_HTML/Xhtml_web page production

Data URI and MHTML completely solve all browsers_HTML/Xhtml_web page production

May 16, 2016 pm 04:42 PM
data uri

Data URI

Data URI is a scheme defined by RFC 2397 to embed small files directly into documents. Through the following syntax, you can convert a small file into a specified encoding and embed it directly into the page:

data:[][;base64],

  1. MIME-type: Specifies the MIME of the embedded data. Its form is [type]/[subtype]; parameter. For example, the MIME corresponding to a png image is image/png. Parameter can be used to specify additional information. In more cases, it is the charset parameter used to specify text encoding methods such as text/plain and text/htm. The default is text/plain;charset=US-ASCII.
  2. base64: The encoding of the data following the statement is base64, otherwise the data must be percent-encoded (that is, urlencode the content).

In the last centuryHTML4.01 introduced the Data URI scheme. As of todayexcept for IE6 and IE7, all major browsers support, but IE8 The support for Data URI is still limited . It only supports object (only for pictures), img, input type=image, link and URL in CSS, and the data size cannot be greater than 32K.

Advantages:

  1. Reduce the number of HTTP requests, there is no TCP connection consumption and the limit on the number of concurrent browsers under the same domain name.
  2. Bandwidth will be reduced for small files. Although the amount of data will increase after encoding, the http header will be reduced. When the amount of data in the http header is greater than the increment of file encoding, the bandwidth will be reduced.
  3. For HTTPS sites, there will be security prompts when using HTTPS and HTTP together. HTTPS is more expensive than HTTP, so Data URI has more obvious advantages in this regard.
  4. You can save the entire multimedia page as a file.

Disadvantages:

  1. Cannot be reused. If the same content is applied to the same document multiple times, it needs to be repeated multiple times, which increases the amount of data and increases the download time.
  2. cannot be cached on its own, so it will be reloaded when its containing document is reloaded.
  3. The client needs to re-decode and display, which increases point consumption.
  4. Data compression is not supported, base64 encoding will increase the size by 1/3, and the data volume will increase even more after urlencoding.
  5. It is not conducive to the filtering of security software, and there are also certain security risks.

MHTML

MHTML is the abbreviation of MIME HTML (Multipurpose Internet Mail Extension HTML), which is a solution defined by RFC 2557 to save all contents of a multimedia page into the same document. This solution was proposed by Microsoft to support it starting from IE5.0, and Opera9.0 also started to support it. Safari can save the file in .mht (MHTML file suffix) format, but does not support displaying it.

MHTML is relatively similar to Data URI. It has more powerful functions and more complex syntax, and does not have the disadvantage of "cannot be reused" in Data URI. However, MHTML is not flexible and convenient to use, such as the URL of resource references. It can be a relative address in the mht file, otherwise it must be an absolute address. hedger's 《Cross Browser Base64 Encoded Images Embedded in HTML》The solution for IE uses a relative path because Content-type: message/rfc822 is declared so that IE will parse it according to MHTML. If the Content is not modified -type requires the use of the MHTML protocol. In this case, the absolute path must be used, such as 《MHTML – when you need data: URIs in IE7 and under》.

Apply

The combination of Data URI and MHTML can completely solve the problem of all mainstream browsers. Since they cannot be cached and reused, they are not suitable for use directly in pages, but they are suitable for use in CSS and JavaScript files. Appropriate use of pictures has great advantages:

  1. Greatly reduce the number of requests. Now the CSS of large websites refers to a large number of image resources.
  2. Both CSS and JavaScript can be cached, indirectly realizing data caching.
  3. Using CSS can solve the problem of data URI reuse
  4. FarewellCSS Sprites, CSS Sprites appeared to reduce the number of requests, but in addition to bringing exceptions in uncertain situations, CSS Sprites also require artificial images Merging, even if there is a merging tool, still requires a lot of time spent on how to effectively puzzle, and brings maintenance difficulties. When you follow certain design principles, you can completely abandon CSS Sprites to write CSS, and finally use tools to convert images into Data URI and MHTML when uploading to the server, such as "Using data-uri to merge style sheets and The tool implemented in python in the picture " can save a lot of time.
  5. Base64 encoding increases the image file by 1/3. Using Data URI and MHTML at the same time is equivalent to an increase of 2/3, but CSS and JavaScript can use gzip compression, which can save 2/3 of the data volume, so use The final data volume after gzip compression is (1 1/3) * 2 * (1/3) = 8/9, so the final traffic is reduced.

In order to facilitate the implementation of Data URI and MHTML in CSS, I wrote a Data URI & MHTML generator. You can see the generated Data URI & MHTML application example.

When using MHTML in a CSS file, the URL must use an absolute path, which is very inflexible, so you can consider using CSS expression to solve it (DEMO), such as:

/*
http://old9.blogsome.com/2008/10/26/css-expression-reloaded/
http://dancewithnet.com/2009/07/27/ get-right-url-from-html/
*/
*background-image:expression(function(ele){
ele.style.backgroundImage = 'url(mhtml:'
document. getElementById('data-uri-css').getAttribute('href',4)
                                                                                                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
How to intercept uri in nginx location How to intercept uri in nginx location May 18, 2023 pm 12:07 PM

Note: The root and aliasroot instructions in location only set the search root to the directory set by root, that is, the uri will not be truncated. Instead, the original uri will be used to jump to the directory to find the file. The aias instruction will truncate the matching uri, and then Use the path set by alias plus the remaining uri as a sub-path to find the uri of proxy_pass in location. If the url of proxy_pass does not have uri, if the tail is "/", the matching uri will be truncated. If the tail is not "/", then Will not truncate the matching uri if the proxy_pass url contains uri

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

What are the Nginx Location directive URI matching rules? What are the Nginx Location directive URI matching rules? May 14, 2023 pm 11:58 PM

1. Introduction The location directive is the core configuration of the http module. It receives requests sent by users based on predefined URL matching rules. Based on the matching results, the request is forwarded to the backend server. Illegal requests are directly rejected and return 403. 404, 500 error handling, etc. 2. Location instruction syntax location[=|~|~*|^~|@]/uri/{…} or location@name{…} 3. URI matching mode The location instruction is divided into two matching modes: 1> Ordinary characters String matching: rules starting with = or without leading characters (~) 2> Regular matching: starting with ~ or ~* indicates regular matching, ~*

What to do if mysql load data is garbled? What to do if mysql load data is garbled? Feb 16, 2023 am 10:37 AM

The solution to the garbled mysql load data: 1. Find the SQL statement with garbled characters; 2. Modify the statement to "LOAD DATA LOCAL INFILE "employee.txt" INTO TABLE EMPLOYEE character set utf8;".

What are the differences between xdata and data What are the differences between xdata and data Dec 11, 2023 am 11:30 AM

The differences are: 1. xdata usually refers to independent variables, while data refers to the entire data set; 2. xdata is mainly used to establish data analysis models, while data is used for data analysis and statistics; 3. xdata is usually used for regression Analysis, variance analysis, predictive modeling, data can be analyzed using various statistical methods; 4. xdata usually requires data preprocessing, and data can contain complete original data.

What is the difference between URL and URI What is the difference between URL and URI Aug 10, 2023 pm 05:15 PM

The difference between URL and URI is: 1. URI is a sequence of strings used to identify resources on the Internet, while URI does not focus on the location of the resource, but only on its identifier; 2. URL provides information about the location of the resource. Detailed information about a location on the Internet, while URI is a broader concept that covers not only URLs, but also other forms used to identify resources, such as URN; 3. URL is a special type of URI, used for location A resource on the Internet, while a URI is a broader identifier used to uniquely identify and name a resource.

More returns than sales: The Humane Ai Pin is becoming a commercial disaster More returns than sales: The Humane Ai Pin is becoming a commercial disaster Aug 08, 2024 pm 01:14 PM

Shortly after the launch of the Humane Ai Pin, scathing reviews revealed that the AI gadget was anything but ready for the market, as most of the originally advertised features either didn't work properly or were simply missing, the battery life was

Can't data in vue component be a function? Can't data in vue component be a function? Dec 19, 2022 pm 05:22 PM

No, data in the vue component must be a function. Components in Vue are used for reuse. In order to prevent data reuse, they are defined as functions. The data data in the vue component should be isolated from each other and not affect each other. Every time the component is reused, the data data should be copied once. Later, when the data data in the component is changed in a reused place, other data will be copied. If the data data of reused local components is not affected, you need to return an object as the status of the component through the data function.

See all articles