Spe*_*May 3 javascript iframe parent reload
我已经在stackoverflow上检查了来自另一个iframe帖子的所有重新加载iframe ...我已经尝试了所有他们的解决方案,但它似乎没有帮助我!所以我的问题是我在同一页面上有两个iframe.iframe的源代码是互相交互的php文件,但我需要iframes重新加载,以便显示更改.我尝试了很多不同的方法(我将在下面列出).这些iframe来自同一个域.也许是其他东西弄乱了这个?提前致谢.
从一个iframe内部调用的不同语句:
parent.document.getElementById(id).src = parent.document.getElementById(id).src;
parent.getElementById(id).location.reload();
Run Code Online (Sandbox Code Playgroud)
试图调用在父窗口中工作的父函数:
内部iframe -
parent.refresh(id);
Run Code Online (Sandbox Code Playgroud)
父窗口工作功能 -
function refresh(id) {
document.getElementById(id).src = document.getElementById(id).src;
}
Run Code Online (Sandbox Code Playgroud)
如果分配name到iframe大多数浏览器可以让你访问iframe的window通过对象name的值.这是指的是一个不同iframe其id属性,它会给你的参考iframe元素本身(从它的所有者document),而不是iframe的内容window.
简单示例:(来自父文档)
<iframe name='iframe1Name' id='iframe1Id'></iframe>
<script>
// option 1: get a reference to the iframe element:
var iframeElem = document.getElementById('iframe1Id');
// update the element's src:
iframeElem.src = "page.html";
// option 2: get a reference to the iframe's window object:
var iframeWindow = window.iframe1Name;
// update the iframe's location:
iframeWindow.location.href = "page.html";
</script>
Run Code Online (Sandbox Code Playgroud)
我们来看看您的代码:
parent.document.getElementById(id).src = parent.document.getElementById(id).src;
Run Code Online (Sandbox Code Playgroud)
如果iframe你使用正确的话,这可以在内部执行id.您可能希望使用调试器来验证是否parent.document.getElementById(id)返回对正确元素的引用,并检查控制台以查看是否抛出任何异常(尝试按F12).由于你没有发布你的完整代码(包括标记),我无法想到告诉你这里的问题是什么.
调试技巧:
parent.location.href以确保您访问您认为自己的窗口,parent.document.getElementId(id)以确保您获得元素对象(与null或相反undefined),parent.document.getElementById(id).tagName以确保使用正确的ID(tagName应==="IFRAME")这一行:
parent.getElementById(id).location.reload();
Run Code Online (Sandbox Code Playgroud)
有两个问题:
getElementById()是一个函数document,但它被称为parent一个window对象,和location是window对象的属性.您正在尝试访问该iframe元素的location属性,该属性不存在.你需要引用iframe's window,而不是它的元素.除了name方法之外,还有其他方法来获取iframe窗口对象:
document.getElementById('iframeId').contentWindow; // for supported browserswindow.frames["iframeName"]; // assumes name property was set on the iframewindow.frames[i]; // where i is the ordinal for the frame如果改变src了的iframe元素不为你工作,这些修复可能:
parent.document.getElementById(id).contentWindow.location.reload();
要么
parent.frames["yourIframeName"].location.reload(); // requires iframe name attribute
要么
parent.frames[0].location.reload(); // frames of immediate parent window
要么
top.frames[0].location.reload(); // frames of top-most parent window
注意:如果使用该name方法,请注意不要使用常用值name,例如"home",因为它与调用的FireFox函数冲突,home()因此如果命名为Firefox,Firefox将不会自动创建对iframe窗口的引用home.最可靠的方法可能是使用,window.frames[]因为我相信已经存在了很长时间(适用于FF/Chrome/Safari/IE6 +(至少))
下面是一个更深入(但非常简单)的示例,在Chrome,FF和IE中进行了测试:
文件#1:frametest.html(父窗口)
<!DOCTYPE html>
<html>
<body>
<iframe id="frame1Id" name="frame1Name" src="frame1.html"></iframe>
<iframe id="frame2Id" name="frame2Name" src="frame2.html"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
文件#2:frame1.html(第1帧的src)
<!DOCTYPE html>
<html>
<body>
FRAME 1
<script>
document.body.style.backgroundColor="#ccc";
setTimeout(function(){document.body.style.backgroundColor="#fff";},100);
document.write(new Date());
</script>
<input type="button" onclick="parent.document.getElementById('frame2Id').src=parent.document.getElementById('frame2Id').src;" value="Refresh frame 2 by ID"/>
<input type="button" onclick="parent.frame2Name.location.reload();" value="Refresh frame 2 by Name"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
文件#3:frame2.html(第2帧的src)
<!DOCTYPE html>
<html>
<body>
FRAME 1
<script>
document.body.style.backgroundColor="#ccc";
setTimeout(function(){document.body.style.backgroundColor="#fff";},100);
document.write(new Date());
</script>
<input type="button" onclick="parent.document.getElementById('frame1Id').src=parent.document.getElementById('frame1Id').src;" value="Refresh frame 2 by ID"/>
<input type="button" onclick="parent.frame1Name.location.reload();" value="Refresh frame 2 by Name"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
此示例演示如何定义和操作iframes by id和by name,以及如何iframe在不同的内部影响s iframe.根据浏览器设置,原始政策可能适用,但您已经说过您的内容全部来自同一个域,因此您应该可以.
| 归档时间: |
|
| 查看次数: |
9565 次 |
| 最近记录: |