我想重新加载一个<iframe>使用JavaScript。到目前为止,我发现最好的方法是将iframe的src属性设置为自身,但这不是很干净。什么好主意吗?
当前回答
对yajra帖子的改进…我喜欢这个想法,但讨厌浏览器检测的想法。
我更倾向于ppk的观点,使用对象检测而不是浏览器检测, (http://www.quirksmode.org/js/support.html), 因为这样你实际上是在测试浏览器的能力,并据此进行操作,而不是你当时认为浏览器的能力。也不需要那么多丑陋的浏览器ID字符串解析,并且不排除您完全不了解的功能完美的浏览器。
不需要看导航器。AppName,为什么不这样做呢,测试你使用的元素呢?(如果你想要更花哨,你可以使用try{}块,但这对我来说很有效。)
function reload_message_frame() {
var frame_id = 'live_message_frame';
if(window.document.getElementById(frame_id).location ) {
window.document.getElementById(frame_id).location.reload(true);
} else if (window.document.getElementById(frame_id).contentWindow.location ) {
window.document.getElementById(frame_id).contentWindow.location.reload(true);
} else if (window.document.getElementById(frame_id).src){
window.document.getElementById(frame_id).src = window.document.getElementById(frame_id).src;
} else {
// fail condition, respond as appropriate, or do nothing
alert("Sorry, unable to reload that frame!");
}
}
这样,您可以根据自己的喜好或需要尝试尽可能多的不同排列,而不会导致javascript错误,并在所有其他方法都失败时执行一些合理的操作。在使用对象之前,对它们进行测试需要做更多的工作,但是,在我看来,这有助于编写更好、更安全的代码。
为我在IE8、Firefox(15.0.1)、Chrome (21.0.1180.89 m)和Windows上的Opera (12.0.2 m)工作过。
也许我可以通过实际测试重载函数做得更好,但这对我来说已经足够了。:)
其他回答
我刚刚遇到这在铬和唯一的工作是删除和替换iframe。例子:
$(".iframe_wrapper").find("iframe").remove();
var iframe = $('<iframe src="' + src + '" frameborder="0"></iframe>');
$.find(".iframe_wrapper").append(iframe);
很简单,其他答案中没有提到。
<script type="text/javascript">
top.frames['DetailFrame'].location = top.frames['DetailFrame'].location;
</script>
新url
location.assign("http:google.com");
assign()方法加载一个新文档。
重新加载
location.reload();
reload()方法用于重新加载当前文档。
从Iframe内部重新加载
如果你的应用程序在Iframe中,你可以通过替换位置href来刷新它:
document.location.href = document.location.href
在IE8中使用。net,设置iframe。SRC第一次是可以的, 而是设置iframe。SRC第二次没有引发iframed页面的page_load。 为了解决这个问题,我使用了iframe.contentDocument.location.href = "NewUrl.htm"。
当使用jQuery thickBox并尝试在thickBox iframe中重新打开同一页面时发现它。 然后它只显示之前打开的页面。
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法