postMessage实现跨域

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

<h1>我是A页面</h1>

<div id="addArea">

</div>

<iframe name="bpage" frameborder="0" scrolling="no" width="300" height="150" src="http://b.com/kuayu/b/postMsg.html"></iframe>

<script type="text/javascript">

 

var addHtml = function(e) {

e = e || window.event;

var data = e.data;

if(data) {

var p = document.createElement('p');

p.innerHTML = data;

document.getElementById('addArea').appendChild(p);

}

};

if(window.addEventListener) {

window.addEventListener('message' , addHtml , false);

} else if(window.attachEvent) {

window.attachEvent("onmessage" , addHtml);

}

</script>

 

 

 

 

 

 

 

 

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

<script type="text/javascript" src="http://js.t.sinajs.cn/STK/js/gaea_1_19.js" ></script>

</head>

<body>

测试页面

<div id="colordiv">

<button action-type="setColor">red</button>

<button action-type="setColor">green</button>

<button action-type="setColor">yellow</button>

</div>

 

 

 

<script>

 

var node = STK.E("colordiv");

var delegate = STK.delegatedEvent(node);

delegate.add("setColor" , 'click' , function(spec) {

var color = spec.el.innerHTML;

window.top.postMessage(color , "*");

});

 

</script>

</body>

</html>

你可能感兴趣的:(message)