带Sharepoint的postMessage iframe调整大小无法正常工作
问题描述:
我试图通过跨域调整Sharepoint环境内的iframe大小。我试着在网络上并没有什么发现一堆解决方案的工作,除了贝克Humadi的附近约postMessage的最终解决方案:带Sharepoint的postMessage iframe调整大小无法正常工作
https://stackoverflow.com/a/15558627/2171556
我添加了一个脚本编辑器到SharePoint页面下面的(我的iframe网址当然指点我的孩子页):
<script>
// Create IE + others compatible event handler
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
// Listen to message from child window
eventer(messageEvent,function(e) {
document.getElementById('frame').height = e.data + 'px';
},false);
</script>
<iframe id="frame" src="childpage" scrolling="no"></iframe>
在我包括以下(RestSection被一些其他方式填充,所以请忽略这个帖子的内容)的子页面:
<script type="text/javascript">
window.onload = function()
{
var actual_height = document.getElementById('RestSection').scrollHeight;
parent.postMessage(actual_height,"childpage");
//* allows this to post to any parent iframe regardless of domain
}
</script>
<div id="RestSection">
//List of things
</div>
当我编辑Sharepoint中的代码片段时,它实际上是按照它应该调整页面的大小。当我检查在它停止工作,我已经注意到了高度设置很奇怪 - 也许有些postMessage的对象?:
<iframe id="frame" src="childpage" scrolling="no" height="{"command":"RequestSuccess","requestIndex":1,"result":null}px"></iframe>
我不明白为什么它的工作原理,当我编辑的页面在Sharepoint但突然当我检查它时不会。有没有人见过这种行为,或者可以在某种程度上启发我?
好的,谢谢,我会在那里重定向我的问题。 – Ghost