Refresh an iframe

南楼画角 提交于 2019-11-30 04:55:25

问题


How to refresh an iframe? For example:

<iframe src="http://google.com"></iframe>

I click on "Videos" in this iframe ,then click on a button to refresh the iframe and I want when the iframe is refreshed ,to be on that "Videos" page.


回答1:


You can't refresh the inner document in there due to the single origin policy.

Have you tried changing the iframe's src property and adding a random GET string to force a reload?

This should work:

<iframe id="myiframe" src="http://google.com"></iframe>

document.getElementById("myiframe").src = "http://www.google.com?"+(+new Date());



回答2:


You can only reload the current iframe href if it's for the same domain (single origin policy) with following code:

<iframe id="myIframe" src="http://yourdomain.com"></iframe>

document.getElementById('myIframe').contentWindow.location.reload(true);

or get the current href

var currentHref = document.getElementById('myIframe').contentWindow.location.href;



回答3:


I think just by updating the hre/src will work. So on button click do as follows:

window.frames.iframeName.location.href = "new url"




回答4:


My solution was to remove previous iframe and append new iframe with new src.



来源:https://stackoverflow.com/questions/3244877/refresh-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!