使用 Sharepoint iframe 调整大小的 postMessage 无法正常工作

postMessage with Sharepoint iframe resize not working correctly

我在尝试通过跨域调整 Sharepoint 环境中的 iframe 时遇到问题。我已经尝试了很多在网上找到的解决方案,除了 Baker Humadi 接近尾声的关于 postMessage:

的解决方案外,没有任何效果

我使用以下内容向 Sharepoint 页面添加了脚本编辑器(我的 iframe URL 当然指向我的子页面):

<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 正在通过其他方式填充,因此请忽略此 post 中的内容):

<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 object?:

<iframe id="frame" src="childpage" scrolling="no" height="{"command":"RequestSuccess","requestIndex":1,"result":null}px"></iframe>

我不明白为什么当我在 Sharepoint 中编辑页面时它可以工作,但当我签入它时突然不工作。有没有人以前见过这种行为或者能以某种方式启发我?

也许你应该在这里寻求帮助是 SharePoint 子网站 https://sharepoint.stackexchange.com/。在编辑模式下工作但不在视图中的脚本是常见的失败。