6

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.

4 Answers4

10

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());
Andy E
  • 338,112
  • 86
  • 474
  • 445
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    doesn't the – poeschlorn Jul 14 '10 at 09:36
  • 4
    +1 - I don't think you need the random string. Just in case you do, I edited your post to append `+new Date()` which will cast the current time to a number. Not random, but unique if you're not refreshing several times per millisecond :-) – Andy E Jul 14 '10 at 09:38
  • `Have you tried changing the iframe's src property` ,yes ,but how to get on which page is the iframe? –  Jul 14 '10 at 10:01
  • 1
    @lam by querying the `src` property? However, that is not possible if the user has navigated to another location in the iframe. In that case, AFAIK, there is no way of refreshing the iframe without refreshing the parent document. – Pekka Jul 14 '10 at 10:03
4

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;
Koen Rijpstra
  • 344
  • 2
  • 8
0

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

skywalker
  • 826
  • 1
  • 10
  • 18
0

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

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

ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75