Using Javascript to override or disable meta refresh tag

后端 未结 8 898
慢半拍i
慢半拍i 2020-11-29 08:08

I have a website, where I am trying to use Ajax to update some stuff on the page without reloading it. However, there is a good chance that many of my users will be using mo

相关标签:
8条回答
  • 2020-11-29 09:10

    Just remove the meta tag with javascript:

    <meta http-equiv="refresh" content="2;http://new-url/" id="meta-refresh">
    
    <script type="text/javascript">
    var mr = document.getElementById("meta-refresh");
    mr.parentNode.removeChild(mr);
    </script>
    

    I've set the refresh timeout to 2 seconds above just as an example. You could probably get away with 1 second as well, but don't set it to 0 because the javascript won't get a chance to execute in that case. 0 is also annoying because it breaks back-button usability.

    Edit 2012-10-23 This does not appear to work any more. The node still gets removed, but it appears that browsers parse and hold in memory all meta tags any way.

    0 讨论(0)
  • 2020-11-29 09:12

    I've found that the noscript tag works quite nicely for this. For example, you can place this just after you close the head element:

    <noscript>
        <meta http-equiv="refresh" content="5;URL=http://www.example.com">
    </noscript>
    

    No need to remove the meta tag with script, since a browser that has script support will ignore everything inside the noscript element.

    0 讨论(0)
提交回复
热议问题