postMessage
<body>
<iframe src="http://localhost:4000/b.html" id="frame" onload="load()"></iframe>
</body>
<script>
function load() {
let iframe = document.querySelector('#frame')
iframe.contentWindow.postMessage('i love you', 'http://localhost:4000')
}
</script>
<body>
window.onmessage = function(e){
console.log(e.data)
e.source.postpostMessageme('i don't love you', e.origin)
}
</body>
复制代码
web worker
let worker = new Worker('./worker.js');
worker.postMessage(1000); // 发消息
worker.onmessage = function (e) { // 接收消息
console.log(e.data); // 消息中的数据
}
console.log(2);
./worker.js
onmessage = function (e) {
let sum = 0;
for(var i = 0;i<e.data;i++){
sum += i;
}
this.postMessage(sum)
}
// 这里不能操作dom
复制代码