Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to refresh an iframe automatically

I would like an iframe to refresh itself to the source path specified. What is the easiest way to do this inline with javascript?

thanks,

like image 345
triq Avatar asked Aug 07 '11 21:08

triq


People also ask

How do I refresh an iframe in JavaScript?

function refresh () { var iframe = document.getElementById ('iframe'); iframe.reload (true); } setTimeout ('refresh ()', 3000); Note, this will try to refresh the page every 3 seconds. Obviously, if the page takes a while to load, i.e. over 3 seconds then you wont see anything.

How to show activity in IFRAME without JavaScript?

If you're also perhaps looking for a way of doing this without javascript enabled, and have access to modify the iframe HTML, then you could add this reload code to the <head> tag of the iframe HTML: Show activity on this post. You can use setInterval (function, milliseconds, param1, param2, ...)

What is an <iFrame> and how to use it?

The <iframe> allows web developers to embed one HTML page inside another. It’s so useful because they’re a part of HTML so you can use them almost anywhere — in website builders like Wix and Squarespace or in your next full-stack project.

Can I insert a meta refresh inside a frame?

However this is not viable if the frame's html is used as another stand alone page & you do not want to insert the 'meta refresh' inside it. The content must be between 30 and 50000 characters. … Download, Vote, Comment, Publish.


2 Answers

setInterval(function(){
    document.getElementById("myframe").src+="";
},2500);
like image 130
gion_13 Avatar answered Sep 18 '22 08:09

gion_13


You can use setInterval(function, milliseconds, param1, param2, ...)

Something like this:

var frameRefreshInterval = setInterval(function() {
    document.getElementById("myframe").src = document.getElementById("myframe").src
}, 2000);

The interval resets the iframe's src attribute ever 2 seconds (2000 milliseconds)

Edit, to answer you comments: This, along with any other code that manipulates the DOM, should be in a window.onload event listener or similar like so:

<script type=text/javascript>
window.onload = function() {
    var frameRefreshInterval = setInterval(function() {
        document.getElementById("myframe").src = document.getElementById("myframe").src
    }, 2000);
    // any other code
}
</script>
like image 40
Griffin Avatar answered Sep 21 '22 08:09

Griffin