简体   繁体   English

禁用 HTML 自动刷新?

[英]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.客户的网站有一个自动刷新功能,非常适合桌面网站,但我正在制作一个 iPhone 应用程序,需要在移动版本上禁用它。

The code used is:使用的代码是:

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

I would like to use javascript to disable it, if possible.如果可能,我想使用 javascript 来禁用它。

Thanks.谢谢。

EDIT: I DO NOT have access to the HTML file, and therefore can't modify it.编辑:我无权访问 HTML 文件,因此无法修改它。 I need to do this via code on the Objective-C side in Xcode.我需要通过 Xcode 中 Objective-C 端的代码来做到这一点。

I will introduce something simple我将介绍一些简单的东西

Add meta tag with ID id="meta-refresh" like this:添加带有 ID id="meta-refresh" meta标记,如下所示:

 <meta http-equiv="refresh" content="2;http://new-url/" id="meta-refresh">

Just use these script..只需使用这些脚本..

 var iOS = false,
     p = navigator.platform;
 if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ){//remove which is not your target
     iOS = true;
 }
 if(iOS){ // check if iOS then do the following
      var mr = document.getElementById("meta-refresh");
      mr.parentNode.removeChild(mr);
 }

I believe this will work..我相信这会奏效..

Also, the initiated request which JavaScript can not disable once loaded!!此外,JavaScript一旦加载就无法禁用的发起请求!! For the same, the exact work around one can find in an Old Post answer given by user XP1同样,可以在用户XP1给出的Old Post答案中找到解决方法

The above is using xmlhttp requrest (AJAX) to check before the document is loaded and remove the meta tag if the device is target device(iphone)以上是使用xmlhttp requrest(AJAX)在加载文档之前检查并删除元标记,如果设备是目标设备(iphone)

OR要么

one can use refresh dynamically id the device is not iPhone/iOS.可以使用动态刷新标识设备不是 iPhone/iOS。 that will remove the requirement of doing the dynamic check and requirement to avoid first refresh call.这将消除进行动态检查和避免首次刷新调用的要求。 without using meta tag不使用元标记

 var iOS = false,
     p = navigator.platform;
 if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ){//remove which is not your target
     iOS = true;
 }
 if(!iOS){
      window.setTimeout(function(){window.location.href=window.location.href},30000); //30 Seconds
 }

short answer: window.location.reload = () => {}简短回答: window.location.reload = () => {}

longer answer: most browsers support some modification of a function that refreshes a web page.更长的答案:大多数浏览器支持对刷新网页的功能进行一些修改。 the one here was tested on Chrome.这里的一个是在 Chrome 上测试的。

Disclaimer: this will remove the page reloading functionality entirely for a given page.免责声明:这将完全删除给定页面的页面重新加载功能。

Assuming you are using a UIWebView to display the site, you could use [webView stringByEvaluatingJavaScriptFromString:@"..."];假设您使用 UIWebView 来显示站点,您可以使用[webView stringByEvaluatingJavaScriptFromString:@"..."]; , where "..." is this javascript: ,其中“...”是这个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.一旦解析器看到元标记,它就会刷新,无论您使用什么 javascript 技巧。 Unfortunately, the only way to overcome this is to modify the HTML page directly.不幸的是,解决这个问题的唯一方法是直接修改 HTML 页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM