Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I stop a META refresh using Javascript? 2

Status
Not open for further replies.

BabyJeffy

Programmer
Sep 10, 2003
4,189
GB
I have a web application that delivers dynamic content to the page. At the moment I have a meta tag based refresh every 300 seconds on that page. I would like to be able to "cancel" the refresh using javascript if certain criteria are met (by the user) on the page (I would fire a call to a function at the stage I want to do this).

I'm developing for FF as well as IE for windows (and degrading support for other browsers and platforms as needed). Ideally any javascript would remain cross-browser at the very least.

I want to keep using the meta refresh (rather than opting for a setTimeout() call) so that my non-javascript users get a chance to use the web app.

Whilst I am using php to deliver this, I don't really want to "sniff" whether javascript is enabled early on in the app if I can avoid it.

Has anyone used Javascript to do this? If so... any suggestions or caveats that you came across?

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
You can pick up on meta tags using:

Code:
document.getElementsByTagName('meta')

This means you can use standard DOM methods to remove the relevant one from the DOM:

Code:
var yourMeta = document.getElementsByTagName('meta')[whichIndex];
yourMeta.parentNode.removeChild(yourMeta);

I have no idea if that will actually stop the refresh from happening or not... so another option might be to use the above to target the timer property and set it to -1.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hmm.. after running some tests, it looks like the DOM manipulation did nothing to stop the refresh. In Firefox / NN, this works:

Code:
window.stop();

but in IE you get an error (the window object does not have a stop method).

I've not tried this, but a possible replacement in IE might be:

Code:
document.execCommand('Stop');

However, I'm not sure what you can do in other browsers (Safari, for example).

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Try this:

Code:
<script>
var tmrRefresh = setTimeout('location.reload(true)', 300 * 1000);

function cancelReload(){
  clearTimeout(tmrRefresh);
}
</script>
[b]<noscript>[/b]
  <META HTTP-EQUIV=Refresh CONTENT="10; URL=http://www.yoursite.com/">
[b]</noscript>[/b]

Adam
 
Oops, should be:
Code:
<script>
var tmrRefresh = setTimeout('location.reload(true)', 300 * 1000);

function cancelReload(){
  clearTimeout(tmrRefresh);
}
</script>
<noscript>
  <META HTTP-EQUIV=Refresh CONTENT="[red]300[/red]; URL=http://www.yoursite.com/">
</noscript>

Adam
 
Thanks all...

Adam, I decided to put the meta tag within a noscript tag (much like your example) -- this then allowed me to use a setInterval arrangement (allowing me to cancel the timer when the criteria are met)... and still support the non-javascript clients (and pass XHTML 1.0 Strict validation).

Dan's suggestions looked promising -- I ended up giving the meta tag an id and then getElementById() to query it. I was able to read (and set) the content property (I tried setting it to -1)... but none of the browsers seemed to honour this. I also tried the suggestion of removing the node from the DOM collection... again to no avail.

I could have opted for several combinations of window.stop and the exec.command stuff... but the final solution (using a setInterval) was ultimately supported on a wider range of browsers and platforms. The end solution is working a treat, regardless.

Thanks again guys... always a pleasure [smile]

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top