【问题标题】:Why isn't the link opening in iframe when I click a button?为什么单击按钮时链接没有在 iframe 中打开?
【发布时间】:2015-07-30 16:18:02
【问题描述】:

我有一个带有下拉列表的 HTML 页面,当我选择一个选项时,会打开一个包含另一个 HTML 页面的 iframe。在第二个 HTML 页面内我有按钮,当我单击该按钮时,我想打开一个链接(在 iframe 内)。当我只运行第二页时它正在工作,但是当我运行第一页(带有下拉列表)时它不再工作了。

有谁知道为什么会发生这种情况,我该如何解决?

这是第一页的代码示例:

function Ex(form) {
            var myindex = Example.drop.selectedIndex
            if (Example.drop.options[myindex] != "0") {
                window.open(Example.drop.options[myindex].value, target = "iframe1");
            }
        }
<form name="Example">
        <select name="drop" onchange="Ex(this.Example)">
            <option value="about:blank">Select something...</option>
            <option value="Page1.html">Page1</option>
            <option value="Page2.html">Page2</option>
            <option value="Page3.html">Page3</option>
            
        </select>


        <iframe id="iframe1" name="iframe1" src="about:blank" align="top" height="900" width="1000" style="border:none; "></iframe>
    </form>

下面是在 iframe 中打开的页面之一的代码示例:

<img src="http://focusdesignz.com/wp-content/uploads/2015/07/picture_1436728897.jpg"/>

<input type="button" onclick="location.href='http://www.google.ro'" value="Some Button"/>

【问题讨论】:

  • 它完全打开了吗?可能是因为 Google 不允许您在 iframe 中打开,它会中断它?
  • @putvande 现在可以使用了。这是谷歌的错。 :)

标签: javascript html iframe


【解决方案1】:

如果不是在 iframe 中,您的代码可以工作。

在 iframe 中,错误消息:[Error] 拒绝在框架中显示“https://www.google.ro/?gws_rd=ssl”,因为它将“X-Frame-Options”设置为“SAMEORIGIN”。 (button.html,第 3 行)

Google.ro 似乎拒绝被 iframe。

如果您使用 Google.ro 以外的其他网址,请确保您的跨域策略正常。 URL 必须同意在 iFrame 中显示。

【讨论】:

  • 我发现它适用于我的页面。我正在打开 HTML 页面,所以它没有任何限制。不管怎样,你的回答很好。
猜你喜欢
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
相关资源
最近更新 更多