如何检测是否通过javascript在浏览器中按下停止加载按钮?

时间:2016-07-30 09:04:54

标签: javascript jquery

如何检测是否通过javascript在浏览器中按下停止加载按钮,或者页面是否仍在加载?

1 个答案:

答案 0 :(得分:0)

假设脚本到达浏览器并且在按下“停止加载按钮”时没有停止执行,这可能是一个可行的选择

使用它仍然可以有非加载资源,但会给你一个良好的开端。

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv='content-type' content='text/html; charset=UTF-8' />
  <script type='text/javascript'>
    
    var DomLoaded = {
      done: false,
      onload: [],
      loaded: function() {
        if (DomLoaded.done) return;
        DomLoaded.done = true;
        if (document.removeEventListener) {
          document.removeEventListener('DOMContentLoaded', DomLoaded.loaded, false);
        }
        for (i = 0; i < DomLoaded.onload.length; i++) DomLoaded.onload[i]();
      },
      load: function(fireThis) {
        this.onload.push(fireThis);
        if (document.addEventListener) {
          document.addEventListener('DOMContentLoaded', DomLoaded.loaded, false);
        } else {
          /*IE<=8*/
          if (/MSIE/i.test(navigator.userAgent) && !window.opera) {
            (function() {
              try {
                document.body.doScroll('up');
                return DomLoaded.loaded();
              } catch (e) {}
              if (/loaded|complete/.test(document.readyState)) return DomLoaded.loaded();
              if (!DomLoaded.done) setTimeout(arguments.callee, 10);
            })();
          }
        }
        /* fallback */
        window.onload = DomLoaded.loaded;
      }
    };

    DomLoaded.load(function() {
      var d = document;
      if (d.getElementsById('loaded-checker')) {
        // loaded

      } else {
        // not loaded

      }
    });
  </script>
  <link rel='stylesheet' type='text/css' href='/css/style.css' />
  <script src="/js/script.js"></script>
</head>

<body>


  <div class="main-header"></div>
  <div class="main-content"></div>
  <div class="main-footer"></div>

  <div id="loaded-checker"></div>

</body>

</html>