@fengfeng
2015-04-25T16:36:14.000000Z
字数 3139
阅读 2242
未分类
(function(window, document) {"use strict";var userData, attr, attributes;if (!window.localStorage && (userData = document.body) && userData.addBehavior) {if (userData.addBehavior("#default#userdata")) {userData.load((attr = "localStorage"));attributes = userData.XMLDocument.documentElement.attributes;window.localStorage = {"length" : attributes.length,"key" : function(idx) { return (idx >= this.length) ? null : attributes[idx].name; },"getItem" : function(key) { return userData.getAttribute(key); },"setItem" : function(key, value) {userData.setAttribute(key, value);userData.save(attr);this.length += ((userData.getAttribute(key) === null) ? 1 : 0);},"removeItem" : function(key) {if (userData.getAttribute(key) !== null) {userData.removeAttribute(key);userData.save(attr);this.length = Math.max(0, this.length - 1);}},"clear" : function() {while (this.length) { userData.removeAttribute(attributes[--this.length].name); }userData.save(attr);}};}}})(this, this.document);
<a href="mailto:someone@example.com?subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body">Send email</a><a href="tel:+468123456">Call</a><a href="sms:+15105550101?body=hello,你好,我是宋史超"> 带中文 </a>
伪协议
是浏览器内部保留协议用于方便访问浏览器的脚本解析引擎和某些内部功能
javascript:
javascript:协议允许后面执行javascript代码,并且继承了调用的当前域。有些情况会对后面的内容处理两次,如果代码正确的话,会把后面的代码当成html解析,覆盖掉原来的html代码,所以很多javascript:void+ 一段代码:
<iframe src='javascript:"hello"'> </iframe>
data:
该协议会创建一个短小的内置式文档,
<!-- 仅一行代码,打造一个在线编辑器 -->data:text/html, <html contenteditable><!-- 图片采用datauri协议,减少请求 --><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD...">
<iframe src="about:blank" name="test"></iframe><script>frames["test"].document.body.innerHTML = "<h1>Hi!</h1>";</script>
在浏览器中,创建一个about:blank页面,它继承的域为创建它的页面的域。例如,点击一个链接,提交一个表单,创建一个新窗口,但是当用户手动输入about:或者书签中打开的话,他的域是一个特殊的域,任何其他的页面都不可以访问。
浏览器通过隔离来自不同源的文件之间的访问来提供各种安全保障。
同源策略一开始是为了管理DOM之间的访问,后来逐渐扩展到Javascript对象,但并非是全部。例如非同源的脚本之间可以调用location.assign()和location.replace()。
把协议-域名-端口 三元素组合在一起组成“源(Origin)",不满足这一条件的被成为跨域。
| 原始资源 | 要访问的资源 | 非IE浏览器 | IE浏览器 |
|---|---|---|---|
| http://example.com/a/ | http://example.com/b/ | 可以访问 | 可以访问 |
| http://example.com/ | http://www.example.com/ | 主机不匹配 | 主机不匹配 |
| http://example.com/a/ | https://example.com/a/ | 协议不匹配 | 协议不匹配 |
| http://example.com:81/ | http://example.com/ | 端口不匹配 | 可以访问 |
同源策略在提高了安全性,但同时也降低了灵活性。
例如很难将login.example.com与payments.example.com两个域之间的数据可以方便的传送
http响应头:HTTP/1.1 200 OKAccess-Control-Allow-Origin:http://another-domain.com