1. ホーム
  2. ジャバスクリプト

jQuery ready メソッドを抽出する

2022-02-09 17:20:27
<パス
(function(){
    document.ready = function(fn){
        if(typeof fn ! == 'function'){
            throw 'Fn is not a function!';
        }
        function completed() {
            document.removeEventListener( "DOMContentLoaded", completed );
            window.removeEventListener( "load", completed );
            fn();
        }
        // Execute directly by determining readyState if it is already loaded, otherwise by listening to the load event, triggered by the event
        if ( document.readyState === "complete" ||
            ( document.readyState ! == "loading" && !document.documentElement.doScroll ) {
            // Handle it asynchronously to allow scripts the opportunity to delay ready
            window.setTimeout( fn );

        } else {

            // Use the handy event callback
            document.addEventListener( "DOMContentLoaded", completed );

            // A fallback to window.onload, that will always work
            window.addEventListener( "load", completed );
        }
    }
})();


JQuery 3.5.1のready関数から抜粋。
jQueryの公式ダウンロードアドレス https://jquery.com/download/