iframe跨域通讯方法
作者:互联网
跨域
a网站主页
<script>
window.onload = function () {
var clientWidth = document.body.clientWidth + 'px';
window.frames[0].postMessage(clientWidth, 'https://www.b.com/');
};
</script>
b网站子页
<script>
window.addEventListener('message', function (e) {
if (e.source != window.parent) return;
var clientWidth = e.data;
$('body').width(clientWidth);
localStorage.setItem('clientWidth', clientWidth);
}, false);
var clientWidth = localStorage.getItem("clientWidth");
if (clientWidth) {
$('body').width(clientWidth);
}
</script>
标签:body,通讯,跨域,clientWidth,width,window,localStorage,iframe,var 来源: https://www.cnblogs.com/huangcong/p/15025978.html