Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 36619  / 11 Years ago, fri, april 19, 2013, 12:00:00

Strange situation:



I am building a menu bar using jQuery and CSS.

In my JavaScript file, I have an on-ready function like so:



$(document).ready(function(e) {
mark_active_menu();
}


and...



function mark_active_menu() {
var elementWidth = $(nav li).width();
alert(elementWidth);
}


For some reason, even BEFORE all the document finish loading, I'm getting the alert message with an incorrect width. Only when I release the message, the rest of the document loads and I'm getting the right width as it should be.



Why my function is being called BEFORE all the document finish loading?
Is there a way to load the function only AFTER a certain element done loading (Example: the nav element)?


More From » jquery

 Answers
21

You can use window.load, it will be triggered after all the resource have completed loading.



$(window).load(function(e) {
mark_active_menu();
});



The load event fires at the end of the document loading process. At
this point, all of the objects in the document are in the DOM, and all
the images and sub-frames have finished loading, Reference



[#78771] Thursday, April 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iselauniquef

Total Points: 443
Total Questions: 98
Total Answers: 102

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
;