直接在 iframe 中设置 src 可以按预期工作
我正在尝试在此处嵌入 Sharepoint 文档。
例如
<iframe src="https://rocketlane123-my.sharepoint.com/personal/lokeshkannan_rocketlane123_onmicrosoft_com/_layouts/15/Doc.aspx?sourcedoc={8822527b-0c56-44f9-8263-40c737db903c}&action=embedview"
width="476px"
height="288px" />
然而,当我在脚本中设置 src 时,它失败了
<iframe id="x" width="476px" height="288px"></iframe>
<script>
document.getElementById('x').src = "https://rocketlane123-my.sharepoint.com/personal/lokeshkannan_rocketlane123_onmicrosoft_com/_layouts/15/Doc.aspx?sourcedoc={8822527b-0c56-44f9-8263-40c737db903c}&action=embedview";
</script>
这种情况在 SharePoint 中明确发生。所以我想在这里了解一些事情。
1. Am I doing something wrong?
2. Is there any CSP headers which block the parent from adding via JS?
3. Is there any official way from SharePoint to allow this?
3. Is there any way to hack this?
提前致谢。
由于这种情况发生在 chrome、safari 和 firefox 上,我认为这不是特定浏览器中的错误。
最佳答案
在 Firefox 中尝试此操作会产生以下错误消息:
To protect your security, login.microsoftonline.com will not allow Firefox to display the page if another site has embedded it. To see this page, you need to open it in a new window.
打开控制台会显示以下消息:
The loading of [url] in a frame is denied by “X-Frame-Options“ directive set to “DENY“.
这是由 login.microsoft.com
设置的 header ,用于禁止将链接嵌入为 iframe。
此链接详细介绍了此设计选择:https://learn.microsoft.com/en-us/sharepoint/troubleshoot/sites/cannot-display-sharepoint-pages-in-iframe
该链接提到您可以通过设置“AllowFraming”来覆盖该行为,但不建议这样做,因为嵌入它可能会导致网站破坏。
可以在 this link 找到使用此功能的指南。
关于javascript - 通过 JS 设置 iframe src 不适用于 sharepoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72999635/