5

I am using jQuery's load function to render some of my content when the document is ready.

$(document).ready(function(){ $('#header').load('header.html',function() { //do call back function}); $('#footer').load('footer.html'); }); 

I don't want my callback function to execute when the request completes, but rather when the loaded content (or dom?) is ready to be manipulated. How can I do this?

Thanks

4
  • 1
    What makes you think that the DOM is not ready to be manipulated in your load() callback? jQuery invokes it after it's dropped the response content into the target container. Commented Jun 21, 2010 at 15:27
  • you are already doing that, do you understand what does $(document).ready does? Commented Jun 21, 2010 at 15:35
  • I am using jQuery's corner function on a span which is in the new content loaded from header.html. The corner function doesn't work correctly unless I add an alert before it, which I thought was a timing issue. If its a timing issue does it makes sense that the content isn't fully loaded when I try to manipulate it? Commented Jun 21, 2010 at 15:38
  • Can you post the code that is not working, and describe exactly what it is that happens? Is it some particular browser that is having problems, or is it the same in all browsers? Commented Jun 21, 2010 at 16:31

2 Answers 2

2

I'm pretty sure your looking to do something after the Window is loaded, not the DOM.

If you have some code the relies on reading the heights and widths of things like images, (document).ready will not work because it is run before the images have been loaded. I've had this problem before when trying to render RSS feeds on a page.

This is the code to do something after the window is loaded:

jQuery(window).load(function() { //Do something after the window is loaded! }); 
Sign up to request clarification or add additional context in comments.

Comments

1

From what I understand the problem here is that the callback gets called as soon as the data is received rather than when the browser has finished rendering it. This is a problem if your callback relies on style properties as one example.

My solution has been to hide the parent node before loading, then adding in a slight delay before showing it again, with a callback placed on the show method.

$('#header').hide().load('header.html').delay(1).show(0, function(){ //callback statements }); 

So this works but isn't bulletproof as we don't know exactly how long the content will take to render.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.