【问题标题】:How to alter iFrames when third parties request the SRC当第三方请求 SRC 时如何更改 iFrame
【发布时间】:2019-03-25 23:28:25
【问题描述】:

为清楚起见进行了编辑:当不同的来源尝试连接时,我想编辑 iFrame 中的元素。

到目前为止我已经尝试过:

在页面上(第三方将从中获取我的小部件的页面) 我已经添加了这个:

function myFunction() {
        var iframe = document.getElementById("myFrame");
        var elmnt = iframe.contentWindow.document.getElementsByTagName("H2") 
        [0];
        elmnt.style.display = "none";
    }

第三方使用的代码是这样的:

<div style="width:80%;">
<iframe id="myFrame" border="0" frameborder="0" 
src="https://example.com"  
frameBorder="0" scrolling="no" style="min-height:1000px;min-width:500px"> 
</iframe>
</div>

正如我所想,这行不通。我很确定 iFrame 实际上是我的内容,我应该能够根据它们是否已连接来修改它们,对吧?

我上面的例子只是为了测试目的,试图移除 H2。

我对任何前端方法或任何使用 C# 的服务器端都持开放态度。

如果有人可以提供帮助,我们将不胜感激。

【问题讨论】:

  • 当您的小部件嵌入到另一个域时,同源策略会阻止从主页访问它的内容
  • 我不想阻止,我想在不同来源尝试连接时编辑元素。
  • 可以设置用户选项并通过 url 查询字符串传递它们并根据这些参数修改服务器端
  • 这不是一个坏主意。感谢您的建议

标签: javascript c# jquery iframe


【解决方案1】:

感谢@charlietfl 的建议。我已经使用参数在 C# 中工作了。

前端:

<h2 id="h2IFrame" runat="server">Hello</h2>

C# 中的服务器端

var activepage = Request.RawUrl;
        if (activepage.Contains("?noh2"))
    {
        h2IFrame.Visible = false;
    }
    else
        h2IFrame.Visible = true;

像这样使用 iFrame:

<div style="width:80%;">
<iframe id="myFrame" border="0" frameborder="0" 
src="https://example.com?noh2"  
frameBorder="0" scrolling="no" style="min-height:1000px;min-width:500px"> 
</iframe>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多