1

I've been using document.getElementById(iframeName).src += ''; to reload my iframe as part of another function that's triggered from the parent page. The only reason I need the iframe page to reload is that I need the javascript on that page to rerun, so I'd much rather reload the iframe from the cache instead of redownloading it from the server. I can't figure out how to make the iframe reload from the cache though.

Also, I'd rather do it without having to reenter the iframe src in the function if possible because I'm using the same function on multiple iframes.

user2856593
  • 97
  • 3
  • 11
  • possible duplicate of [What's the best way to reload an iframe using JavaScript?](http://stackoverflow.com/questions/86428/whats-the-best-way-to-reload-an-iframe-using-javascript) –  Oct 27 '14 at 15:19

1 Answers1

2

You can re-assign the same src to itself. That will do the trick. See below:

# frame.html

//your data  
<script> alert("yo!"); </script>

# yourPage.html

<iframe src="frame.html" id="myFrame"></iframe>
<button id="btn">Reload Iframe</button>

# your javascript for yourPage.html

var btn = document.getElementById("btn");
var frame = document.getElementById("myFrame");
var reloadFrame = function () {
    frame.src = frame.src;
}
btn.addEventListener("click", reloadFrame);

That should do the trick. The browser reloads elements like images, iframes etc. whenever a re-assignment to thier src or href attribute occurs.

user3459110
  • 6,961
  • 3
  • 26
  • 34