Technologies:
Tolerim
a month ago
How come onclick doesn't reload iframe?
<iframe>
using JavaScript. Although I have come across a method that involves setting the src
attribute of the iframe to itself, it may not be the most elegant solution. Do you have any suggestions for a cleaner approach?Answers(2)
Vogi
a month ago
Verified Answer
Yes, there are several ways to reload an iframe using JavaScript. One way is to set the
src
attribute of the iframe to itself. This can be done by calling iframe.contentWindow.location.reload();
after getting the iframe with getElementById
. Another way is to reset the src
attribute with a time stamp for cache busting like this: iframe.src = iframe.src.split(\"?\")[0] + \"?_=\" + new Date().getTime();
. A third way is to clear the src
when query strings option is not possible (Data URI) like this: var wasSrc = iframe.src;
iframe.onload = function () {
iframe.onload = undefined;
iframe.src = wasSrc;
}
I hope this helps! Let me know if you have any other questions.Sources
How to refresh an iframe using JavaScript? - The Web DevHow to refresh an IFrame using Javascript? - Stack OverflowHow to refresh an iframe using JavaScript? - Pinoriajsp - Reload iframe using javascript - Stack OverflowTolerim
a month ago
Yes, you can use the location.reload() method to reload the contents of an element using JavaScript. Here's an example:
var iframe = document.getElementById('your-iframe-id');
iframe.contentWindow.location.reload(true);
In this example, we first select the element using its ID. Then, we access its contentWindow property to get its window object, and call the location.reload() method on it to reload the contents of the iframe. The optional boolean parameter true is used to force a reload from the server instead of the cache.