Reputation: 106
How do I auto Refresh a Iframe every 3 Seconds with out it refreshing the entire page. I use <meta http-equiv="refresh" content="1">
but it shows the entire page refresh and puts you to the top of the page each time it does. My Iframe is pointed at a text file to read live messages I put in. Is there even a way to do this with out it refreshing the entire page, just the element. I am a HTML beginner so explain thoroughly please. I google alot and tried many things and so far nothing works and I try in many browser.
My code So Far:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" />
<title>Chat</title>
<style>
body{
margin: 0px;
}
h1{
font-family: arial;
font-size: 100%;
}
iframe{
width: 900px;
height: 500px;
}
div.top{
background-color: rgba(2, 109, 8, 0.83);
height: 30px;
width: 100%;
}
</style>
</head>
<body>
<div class="top">
<img src="favicon.png" alt="favicon" height="31" width="31" >
<h1>Chat</h1>
</div>
<iframe src="text.txt" name="iframe_a"> />
</body>
</html>
Upvotes: 5
Views: 66226
Reputation: 11
<head>
<script>
function refreshIFrame() {
var x = document.getElementById("*Your_iframe_id*");
x.contentWindow.location.reload();
var t = setTimeout(refreshIFrame, 3000);
}
</script>
</head>
<body onload="refreshIFrame()">...
<iframe id="*Your_iframe_id*" src= ...></iframe>...
</body>
this solution worked for me, I discovered that the 'refreshIFrame()' function was entered as a parameter without the braces () in the following line: " var t = setTimeout(refreshIFrame, 3000); ". Just substitute the relevant values and you are good to go.
Upvotes: 1
Reputation: 1742
You have problems with your html tags. (the "iframe" tag is malformed)
Here you have your code fixed and your answer ready to run:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" />
<title>Chat</title>
<style>
body {
margin: 0px;
}
h1 {
font-family: arial;
font-size: 100%;
}
iframe {
width: 900px;
height: 500px;
}
div.top {
background-color: rgba(2, 109, 8, 0.83);
height: 30px;
width: 100%;
}
</style>
</head>
<body>
<div class="top">
<img src="favicon.png" alt="favicon" height="31" width="31">
<h1>Chat</h1>
</div>
<iframe id="iframe" src="text.txt"></iframe>
<script>
window.setInterval(function() {
reloadIFrame()
}, 3000);
function reloadIFrame() {
console.log('reloading..');
document.getElementById('iframe').contentWindow.location.reload();
}
</script>
</body>
</html>
Regards
Upvotes: 4
Reputation: 39
You could accomplish what you need by simply using a few lines of Javascript.
<script>
window.setInterval("reloadIFrame();", 3000);
function reloadIFrame() {
document.frames["frameNameHere"].location.reload();
}
</script>
Upvotes: -1