$(document).ready() vs. $(window).load(function()

https://learn.jquery.com/using-jquery-core/document-ready/

Първото (ready) се стартира когато HTML-ът е зареден, без значение дали картинките например са заредени.

Второто (load) – когато цялата страница е заредена – картинки и прочее.

A page can’t be manipulated safely until the document is „ready.“ jQuery detects this state of readiness for you. Code included inside $(document).ready() will only run once the page DOM is ready for JavaScript code to execute. Code included inside $(window).on("load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.

https://stackoverflow.com/questions/4584373/difference-between-window-load-and-document-ready-functions

  • document.ready is a jQuery event, it runs when the DOM is ready, e.g. all elements are there to be found/used, but not necessarily all content.
  • window.onload fires later (or at the same time in the worst/failing cases) when images and such are loaded, so if you’re using image dimensions for example, you often want to use this instead.
$(document).ready(function() {
    // executes when HTML-Document is loaded and DOM is ready
    alert("document is ready");
});
$(window).load(function() {
    // executes when complete page is fully loaded, including all frames,
    // objects and images
    alert("window is loaded");
});

Вашият коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *