views:

7840

answers:

6

I would like to reload an iframe using javascript. The best way I found until now was set the iframe src attibute to itself, but isn't pretty. Any ideas?

+4  A: 

window.frames['frameNameOrIndex'].location.reload();

scunliffe
+5  A: 

document.getElementById('some_frame_id').contentWindow.location.reload();

be careful, in Firefox, window.frames[] cannot be indexed by id, but name or index

Ed
This won't work if it is not on the same domain.
aemkei
+1  A: 

If you want it to reload at set intervals, you can also just add a meta tag like this:

<meta http-equiv="refresh" content="60" />

where the number is how many seconds before it refreshes itself.

Jarett
+1  A: 

In IE8 using .Net, setting the iframe.src for the first time is ok, but setting the iframe.src for the second time is not raising the page_load of the iframed page. To solve it i used iframe.contentDocument.location.href = "NewUrl.htm".

Discover it when used jQuery thickBox and tried to reopen same page in the thickbox iframe. Then it just showed the earlier page that was opened.

Shaulian
you just saved my bacon with that one - awesome
Dr. Frankenstein
anytime yaya ..
Shaulian
A: 

Because of the same origin policy, this won't work when modifying an iframe pointing to a different domain. If you can target newer browsers, consider using HTML5's Cross-document messaging. You view the browsers that support this feature here: http://caniuse.com/#feat=x-doc-messaging.

If you can't use HTML5 functionality, then you can follow the tricks outlined here: http://softwareas.com/cross-domain-communication-with-iframes. That blog entry also does a good job of defining the problem.

big lep
A: 

document.getElementById('iframeid').src=document.getElementById('iframeid').src

It will reload the iframe even across domains! Tested with IE7/8,Firefox and Chrome.

evko