阻止使用JS刷新网站

Preventing site from refreshing using JS

本文关键字:刷新 网站 JS      更新时间:2023-12-29

一些网站使用一些JS魔法自动刷新,这让人讨厌。这似乎绕过了浏览器的"不自动刷新"选项。

我的问题是,有没有一种简单的方法可以告诉浏览器(也可以通过JS,在附加组件中)"不要听他们的!永远不要自动刷新!"?

编辑:我想写一个浏览器扩展插件,防止当前页面发出"刷新"信号,无论是自动刷新还是页面使用计时器自动发出的"标准"刷新。我认为这是一个标准的JS问题。。。

编辑:对不起,我答错了问题

我认为你需要的是:

window.onkeypress = window.onkeyup = window.onkeydown = function( event ) {
    event.preventDefault();
    // or, in this case: 
    //return false;
};

当窗口将接收到按键事件时,keyup和keydown表示必须阻止默认行为。如果返回false,则会停止事件链,从而阻止执行任何操作。这两行有相同的效果,试着评论每一行。

这里有一个改进的例子:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Testing...</title>
    </head>
    <body>
        <div id="clickDiv">Click me ;)</div>
        <br/>
        <input id="typeInput" value="Type something ! (here)"/>
    </body>
    <script type="text/javascript">
            window.onkeypress = window.onkeyup = window.onkeydown = function( event ) {
                event.preventDefault();
                return false;
            };
            document.getElementById( "clickDiv" ).onclick = function( event ) { 
                 console.log( "you clicked me and the window events related to the keyboard are still being prevented ;)" );
            };
            document.getElementById( "typeInput" ).onkeyup = function( event ) { 
                 console.log( "you typed inside me and the window events related to the keyboard are still being prevented ;)" );
            };
    </script>
</html>

除Chrome和Safari外,所有流行的浏览器都有禁用自动刷新的选项。要在这些浏览器中禁用自动刷新,请查看本文:http://maketecheasier.com/disable-web-page-auto-refresh-for-various-browsers/2010/12/12

对于停止自动刷新的Chrome扩展,请查看以下内容:https://chrome.google.com/webstore/detail/lcldcllmbokpbniijpnkpgoboadbfphb

每个浏览器都可以关闭此功能,但我认为除非编写"扩展",否则没有办法通过JS影响这些更改。