这是demo,您可以发布消息使用代码,如下所示:
addEvent(document.getElementsByTagName('form')[0], 'submit', function (e) {
if (e.preventDefault)
e.preventDefault();
win.postMessage(
document.getElementById("message").value,
"http://html5demos.com"
);
// otherwise set the returnValue property of the original event to false (IE)
e.returnValue = false;
return false;
});
但为什么这有用呢?
我们应该在哪个地方使用postMessage
?
答案 0 :(得分:1)
这是关于Web Workers的,它们非常有用。
如果您使用带有Javascript的普通网页进行大量计算,您的浏览器将冻结,直到计算完成。但是,如果您使用Web Workers,则浏览器不会冻结,因为代码是在单独的线程上执行的。
在html5demos的示例中,iFrame用作后台工作程序。有关更好(和更有用)的示例,请查看John Resig的blog post