javascript - 通过 JS 设置 iframe src 不适用于 sharepoint

标签 javascript iframe sharepoint sharepoint-online content-security-policy

直接在 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}&amp;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}&amp;action=embedview";
</script>

Passing vs failing

这种情况在 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/

相关文章:

javascript - 2 列布局,如何使用 CSS 使一列与其他列的高度相匹配

javascript - 如果空白,则在 HTML 表单上设置默认字段值

ios - 在 iOS 上通过(外部)javascript 启动 YouTube iframe 播放器

javascript - 如何在 SharePoint 托管应用程序中使用 javascript REST API 发送附件并设置电子邮件优先级?

javascript - setLngLat().setPopup 不是函数

javascript - 没有jquery的HTML文档分页

javascript - 我可以阻止用户将 Javascript 粘贴到设计模式 IFrame 中吗?

javascript - 防止父页面向下滚动到 iframe

javascript - 从 displaform.aspx 隐藏 id 或 div

c# - IHttpHandler 不会接受 DELETE 或 PUT 请求