【问题标题】:Refresh iframe without redirecting parent page刷新 iframe 而不重定向父页面
【发布时间】:2015-03-11 06:42:43
【问题描述】:

我有网站 A,我将它嵌入到网站 B 的 iframe 中。网站 A 有一个下拉菜单,可以从中更改网站的语言。

问题:当我从 iframe 内部的下拉菜单中更改语言时,整个父窗口重定向到网站 A。我希望刷新 iframe 并更改语言,但不让父窗口重定向.这是当前嵌入 iframe 的 html:

<iframe  src="xxx" scrolling="no" frameborder="no" align="center" height = "600" width = "940">
</iframe>

我尝试使用 iframe 沙盒属性,结果如下:

sandbox="allow-forms allow-scripts" - 父页面不重定向,点击下拉菜单后 iframe 显示空白页面

sandbox="allow-forms allow-scripts allow-same-origin" - 同上

sandbox="allow-forms allow-scripts allow-same-origin allow-top-navigation" - 页面重定向没有沙盒属性

对于前两个,我还从控制台注意到我收到了这条消息:

不安全的 JavaScript 尝试从 URL 为“yyy”的框架启动对 URL 为“xxx”的框架的导航。试图导航*窗口的框架是沙盒的,但未设置“allow-top-navigation”标志。

这是否表明要解决此问题,我必须更改 iframe 内页面上的某些内容,而不是父页面?有什么想法吗?

【问题讨论】:

标签: javascript html iframe sandbox


【解决方案1】:

您是否尝试在更改选择值时将target 属性设置为_self

也就是说…… 页面 A:

VARS: <script>document.write(document.location.search)</script><br>
<br>
<form name="f" target="_self">
    <select id="langSelect" name="langSelect" onChange="document.f.submit()">
        <option value="">...please select...</option>
        <option value="en">English</option>
        <option value="es">Spanish</option>
    </select>
</form>

B页:

This if page B...<br>
<br>
This is iframe A:<br>
<iframe src="A.html"></iframe>

【讨论】: