...

View Full Version : Need Help with Javascript Refresher



taylerzy
01-02-2012, 05:00 AM
I need a Javascript refresh script that can refresh a page after a certain amount of milliseconds, but I want the script to stop refreshing when there is a certain tag or id on the page such as <div id="blue"> or something like that.

How can I go about doing this?

DanInMa
01-02-2012, 05:17 AM
I modified this: http://www.javascriptkit.com/script/script2/autofresh.shtml

should do the trick


<script type="text/javascript">
<!--

/*
Auto Refresh Page with Time script
By JavaScript Kit (javascriptkit.com)
Over 200+ free scripts here!
*/

//enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
var limit = "0:30"

if (document.images) {
var parselimit = limit.split(":")
parselimit = parselimit[0] * 60 + parselimit[1] * 1
}

function beginrefresh() {
if (!document.images) return
if (!document.getElementById("blue") {
if (parselimit == 1) window.location.reload()
else {
parselimit -= 1
curmin = Math.floor(parselimit / 60)
cursec = parselimit % 60
if (curmin != 0) curtime = curmin + " minutes and " + cursec + " seconds left until page refresh!"
else curtime = cursec + " seconds left until page refresh!"
window.status = curtime
setTimeout("beginrefresh()", 1000)
} else {
window.status = "Blue div found";
}
}
}

window.onload = beginrefresh
//-->
</script>

taylerzy
01-02-2012, 06:07 AM
Thanks, but I still can't seem to get it to work? That's the exact effect I'm looking for, but on a page like this, nothing happens in Firefox 9:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/loose.dtd">
<html>
<head>
<script type="text/javascript">
<!--

/*
Auto Refresh Page with Time script
By JavaScript Kit (javascriptkit.com)
Over 200+ free scripts here!
*/

//enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
var limit = "0:10"

if (document.images) {
var parselimit = limit.split(":")
parselimit = parselimit[0] * 60 + parselimit[1] * 1
}

function beginrefresh() {
if (!document.images) return
if (!document.getElementById("blue")) {
if (parselimit == 1) window.location.reload()
else {
parselimit -= 1
curmin = Math.floor(parselimit / 60)
cursec = parselimit % 60
if (curmin != 0) curtime = curmin + " minutes and " + cursec + " seconds left until page refresh!"
else curtime = cursec + " seconds left until page refresh!"
window.status = curtime
setTimeout("beginrefresh()", 1000)
} else {
window.status = "Blue div found";
}
}
}

window.onload = beginrefresh
//-->
</script>
</head>
<body>
This page should refresh after 10 seconds unless the id blue is on the page. Since there is no id named blue on this page, it should be refreshing..?
</body>

Old Pedant
01-02-2012, 08:51 AM
Why so complex? K.I.S.S.



<html>
<head>
<script type="text/javascript">
function initRefresh( )
{
if ( document.getElementById("blue") == null )
{
setTimeout( window.location.reload, 5000 );
}
}
window.onload = initRefresh;
</script>
</head>
<body>
.... may or may not have <div id="blue"> ...
</body>
</html>

taylerzy
01-04-2012, 06:40 AM
Why so complex? K.I.S.S.



<html>
<head>
<script type="text/javascript">
function initRefresh( )
{
if ( document.getElementById("blue") == null )
{
setTimeout( window.location.reload, 5000 );
}
}
window.onload = initRefresh;
</script>
</head>
<body>
.... may or may not have <div id="blue"> ...
</body>
</html>


Wouldn't this script refresh the page once and then stop?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum