you don't know html
Opened this issue · 0 comments
MengZhaoFly commented
引入跨域资源的标签 属性 crossorigin
一个协商跨域的值:MDN-attr-crossorigin
- anonymous
如果使用这个值的话就会在请求中的header中的带上Origin属性。
for example:
标签 image 如果服务端未设置Access-Control-Allow-Origin HTTP header, 图片将会 error.
标签 script 中,如果没有设置 CORS, 跨域 js 发生错误,将会向 window.error,提供很少的信息,通过 crossorigin 就能得到详细的信息。
<script src="http://localhost:9999/test.js" crossorigin="anonymous"></script>
- use-credentials
请求的时候将会带上:cookie, certificate, or HTTP Basic authentication。
localStorage
同一个域名document.domain
共享同一个localStorage, localStorage 存在溢出问题。
解决方式:可通过 iframe + postMessage 扩容
for example: set
function setAnotherOriginLocalStorage(key, value) {
iframe.postMessage({key, value, operation: 'set'}, 'http://localhost:9999');
}
setAnotherOriginLocalStorage('key', {s: 'string'})
for example: get
getAnotherOriginLocalStorage('key', (err, res) => {
console.log('get key:', res);
});
function getAnotherOriginLocalStorage(key, cb) {
window.onmessage = (e) => {
console.log(1111);
var localStorageData = e.data;
cb(null, localStorageData);
}
iframe.postMessage({key, operation: 'get'}, 'http://localhost:9999');
}
cookie domain
默认为当前的请求地址
假设 baidu.com 产生的 cookie要让子域 a.baidu.com b.baidu.com 访问,就需要 domain。
script async
defer: 延缓 解析html同时加载js,在解析完元素之后,DOMContentLoaded 之前执行,且按照在文档中出现的顺序执行;
async: 异步 解析html同时加载js,加载完js之后就执行,无顺序;
一图胜千言