자바 스크립트에서 문서 준비가 완료 될 때까지 기다렸다가 본문 하단에 div를 삽입해야합니다.
내가하고 싶은 :
- 메이크업이 자바 스크립트가 가능한 한 작은 파일을 보내지 않고
- 인라인 폐쇄에 문서 readyness을 제공하는 코드 ((< 1킬로바이트 가능한 경우에 그것을 아래로 컴파일))
내 파일에서 전체 jQuery 소스를 인라인하는 것이 너무 커서 다른 방법을 찾고 있습니다. window.onload
가 작동하지만 문서 준비가 필요하며 window.onload
이벤트를 기다리지 않아야합니다.
누구나이 작업을 수행 할 수있는 JS 스 니펫을 알고 있습니까? 아니면 jQuery 소스의 일부만 복사해야합니까?
편집 :
var ready = (function() { var ready_event_fired = false; var ready_event_listener = function (fn) { // Create an idempotent version of the 'fn' function var idempotent_fn = function() { if (ready_event_fired) { return; } ready_event_fired = true; return fn(); } // The DOM ready check for Internet Explorer var do_scroll_check = function() { if (ready_event_fired) { return; } // If IE is used, use the trick by Diego Perini // http://javascript.nwbox.com/IEContentLoaded/ try { document.documentElement.doScroll('left'); } catch(e) { setTimeout(do_scroll_check, 1); return; } // Execute any waiting functions return idempotent_fn(); } // If the browser ready event has already occured if (document.readyState === "complete") { return idempotent_fn() } // Mozilla, Opera and webkit nightlies currently support this event if (document.addEventListener) { // Use the handy event callback document.addEventListener("DOMContentLoaded", idempotent_fn, false); // A fallback to window.onload, that will always work window.addEventListener("load", idempotent_fn, false); // If IE event model is used } else if (document.attachEvent) { // ensure firing before onload; maybe late but safe also for iframes document.attachEvent("onreadystatechange", idempotent_fn); // A fallback to window.onload, that will always work window.attachEvent("onload", idempotent_fn); // If IE and not a frame: continually check to see if the document is ready var toplevel = false; try { toplevel = window.frameElement == null; } catch (e) {} if (document.documentElement.doScroll && toplevel) { return do_scroll_check(); } } }; return ready_event_listener; })(); // TEST var ready_1 = function() { alert("ready 1"); }; var ready_2 = function() { alert("ready 2"); }; ready(function() { ready_1(); ready_2(); });
는 당신에게 jQuery를 소스에서 발견이 날 도와 주셔서 대단히 감사합니다 : 나는 jQuery를 소스를 크롤링하고 다음 코드와 함께 넣어 관리
. 이제 모든 기능을 종료하고 전역 범위를 오염시키지 않고 모든 작업을 종료하고 작업을 수행 할 수 있습니다.
당신은 당신이 이미 cachedd되는 좋은 변화를 줄 것 구글의 서버에서 jQuery를 소스를 가져올 수 있습니다. – Pointy