代码之家  ›  专栏  ›  技术社区  ›  dacracot

需要使用body onload事件,但第三方JavaScript库劫持了它

  •  0
  • dacracot  · 技术社区  · 16 年前

    <body onload="initComments('myID')">
    

    但明喻似乎出于自己的目的劫持了onload,因此initComments('myID')永远不会执行。

    除了更改明喻代码外,我如何让我的初始值设定项运行?

    我不希望仅仅为了解决这个问题而添加另一个库(即jQuery)。

    6 回复  |  直到 10 年前
        1
  •  2
  •   Justin Johnson    16 年前

    第二,明喻很可能会覆盖 onload

    使用jQuery:

    <script src="/js/blah/simile.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            initComments('myID');
        });
    </script>
    

    未使用库(找到) here ):

    <script src="/js/blah/simile.js" type="text/javascript"></script>
    <script type="text/javascript">
      function addOnloadEvent(fnc){
        if ( window.addEventListener ) {
          window.addEventListener( "load", fnc, false );
        } else if ( window.attachEvent ) {
          window.attachEvent( "onload", fnc );
        }
        else {
          var oldOnload = window.onload || function() {};
          window.onload = function(e) {
            oldOnload(e);
            window[fnc](e);
          };
        }
      }
    
      addOnloadEvent(function() { initComments('myID'); });
    </script>
    
        2
  •  1
  •   Matt Ball    16 年前

    使用jQuery的 $(document).ready 事件,它允许您添加任意数量的处理程序(与 onload ).

        3
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    16 年前

    外接程序jQuery和use

    $(document).ready(function(){
      // Your code here...
    });
    

    如果您正在进行任何类型的JavaScript开发,那么 需要 查看jQuery。真是太好了!

        4
  •  1
  •   Mogsdad    10 年前

    // Add this to top of your script.
    window.onload = function () {
        for (var i = 0; arguments.callee.functions.length > i; i++) {
            arguments.callee.functions[i]();
        }
    };
    window.onload.functions = [];
    
    // Now for every onload function you want to add, do:
    window.onload.functions.push(initComments('myID'));
    
        5
  •  0
  •   Community Mohan Dere    8 年前

    被劫持,你的意思是,明喻在你的代码之后加载并覆盖 onload ? 在这种情况下,确保你支持它(作为贾斯汀) says ),并且在您自己覆盖它之前,存储 装载

    也就是说,如果不使用jQuery。

        6
  •  0
  •   James Black    16 年前

    如果您看这里,您将看到jQuery使用的解决方案,我相信,或者是对它的修改,但它应该适合您的需要。

    http://dean.edwards.name/weblog/2006/06/again/

    // Dean Edwards/Matthias Miller/John Resig
    
    function init() {
      // quit if this function has already been called
      if (arguments.callee.done) return;
    
      // flag this function so we don't do the same thing twice
      arguments.callee.done = true;
    
      // kill the timer
      if (_timer) clearInterval(_timer);
    
      // do stuff
    };
    
    /* for Mozilla/Opera9 */
    if (document.addEventListener) {
      document.addEventListener("DOMContentLoaded", init, false);
    }
    
    /* for Internet Explorer */
    /*@cc_on @*/
    /*@if (@_win32)
      document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
      var script = document.getElementById("__ie_onload");
      script.onreadystatechange = function() {
        if (this.readyState == "complete") {
          init(); // call the onload handler
        }
      };
    /*@end @*/
    
    /* for Safari */
    if (/WebKit/i.test(navigator.userAgent)) { // sniff
      var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
          init(); // call the onload handler
        }
      }, 10);
    }
    
    /* for other browsers */
    /*window.onload = init; */
    

    推荐文章