worker.html
<html>
<head>
</head>
<body>
<span id="state" style="background-color:#09F"> </span>
<script>
var state=document.getElementById('state');
if(window.Worker){
var worker=new Worker('worker.js');
//向worker发送消息
worker.postMessage("john");
worker.onmessage=function(event){
state.textContent+=event.data;
};
}else{
alert("浏览器不支持web worker!");
}
</script>
</body>
</html>
worker.js
onmessage=function(event){
if(event.data="john"){
postMessage("嗨!"+event.data+",收到了你的消息");
}
};