Home > php教程 > php手册 > Data URI scheme

Data URI scheme

WBOY
Release: 2016-06-13 09:33:39
Original
1854 people have browsed it

-----------------------------------------------------------------------------------------------

Data URI scheme is a URI scheme(统一资源定位符的模式)。

我们熟知的站点引用资源文件,都是类似http://www.chenwei.ws/demo.png这种的格式,但是data URI scheme 提供将外部资源嵌入到网页中的方法。

它是一种文件或文档的形式,这项技术允许普通的元素诸如要取的图片,样式表放在单一的http请求中,替代很多http请求。(相当于变成网页的一部分了)

------------------------------@author chenwei --------------------

用法=>

HTML:

<span><</span><span>img </span><span>src</span><span>="data:image/png;base64,iVBORw0Rw0KGgoAA......"</span><span>></span>
Copy after login

CSS:

<span>div </span>{<span>background</span>:<span> red url('data:image/png</span>;<span>base64,iVBORw0Rw0KGgoAA......') no-repeat scroll left top;</span>}
Copy after login

JavaScript:(IE8不支持)

<script><span>
window.open(</span>'data:text/html;charset=utf-8,' +<span> encodeURIComponent(
    </span>'<!doctype html>' +
    'html lang='en'<span> +
    </span>'<head><title>Embedded Window</title></head>'<span> +
    </span>'<body><h2>42</h2></body>'<span> +
    </span>'</html>'
<span>  );
</span></script>
Copy after login

data 表示数据名称,image/png 是数据类型名称; base64 是数据编码方法,后面是base64编码后的数据。

Data URI scheme支持的类型有:
data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据

 

注:PHP中可以用函数base64_encode()进行编码,base64_encode(file_get_contents('demo.png'));

复制下面数据到浏览器中,看看是什么吧。

data:text/plain;base64,d3d3LmNoZW53ZWkud3M=

-----------------------------------------------------------------------------------------------

参考Wiki: http://en.wikipedia.org/wiki/Data:_URL

Related labels:
php
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template