inner.html
<html>
<head>
</head>
<body>
<span id="state" style="background-color:#09F"> </span>
<script>
var state=document.getElementById('state');
if(window.SharedWorker){
var worker=new SharedWorker('worker.js');
worker.port.onmessage=function(event){
state.textContent+=event.data;
};
}else{
alert("浏览器不支持web worker!");
}
</script>
</body>
</html>
worker.html
<html>
<head>
</head>
<body>
<span id="state" style="background-color:#09F"> </span>
<script>
var state=document.getElementById('state');
if(window.SharedWorker){
var worker=new SharedWorker('worker.js');
worker.port.onmessage=function(event){
state.textContent+=event.data;
};
}else{
alert("浏览器不支持web worker!");
}
</script>
<br/><br/>
<iframe src="inner.html"></iframe>
</body>
</html>
worker.js
var count=0;
onconnect=function(event){
count+=1;
var port=event.port[0];
//向页面发送消息
port.postMessage("嗨!这是链接#"+count);
};