Search code examples
javascripthtmliphonerefresh

Disable HTML auto-refresh?


A client's website has an auto-refresh feature which works great for desktop sites, but I'm making an iPhone app and need to disable it on the mobile version.

The code used is:

<META HTTP-EQUIV="REFRESH" CONTENT="30">

I would like to use javascript to disable it, if possible.

Thanks.

EDIT: I DO NOT have access to the HTML file, and therefore can't modify it. I need to do this via code on the Objective-C side in Xcode.


Solution

  • Assuming you are using a UIWebView to display the site, you could use [webView stringByEvaluatingJavaScriptFromString:@"..."];, where "..." is this javascript:

        var metaTags = document.getElementsByTagName("META");
        for(var i = 0; i < metaTags.length; i++) {
            if (metaTags[i].getAttribute("HTTP-EQUIV").match(/^REFRESH$/i))
                metaTags[i].parentNode.removeChild(metaTags[i]);
        }
    

    Condensed down to one line for convenience:

    var metaTags = document.getElementsByTagName("META"); for(var i = 0; i < metaTags.length; i++) { if (metaTags[i].getAttribute("HTTP-EQUIV").match(/^REFRESH$/i)) { metaTags[i].parentNode.removeChild(metaTags[i]);}}
    

    Edit: Well...after all that, turns out it's not possible to cancel an existing refresh request by simply removing the meta tag from the document. Once the parser sees the meta tag, it will refresh regardless of any javascript trickery you do. Unfortunately, the only way to overcome this is to modify the HTML page directly.