views:

311

answers:

3

I have a website that I'm working on. I'm using jquery to animate and display content. This content is stored in vars. I need to know how to load the content before displaying it.

For clarification, you click a link, a "loading..." window fades in, and once it loads everything, it fades out and fades in the loaded content that is stored in vars.

Thank you

A: 

Are you looking for how to request HTML content via AJAX, know when it is finished, and then insert it into the DOM? If so, jQuery's load method may be what you're after.

Steve

Steve Harrison
A: 

AJAX event will not tell you how many percent was loaded, in fact, in most cases, it has no idea how long is the response will be. But it will inform you when the response is completed, or error occured.

Take a look at the official reference AJAX of JQuery. My original answer was wrong, coz I suppose you already have the data. A simplified use case for your ajax request would be:

> Initiate the Request, and set the handler for ajax complete (thru something like $.Ajax)
> Hide the content pane and show the loader
> When ajax complete, you display your content, and hide the loader


Following is the original answer.


I think you are talking about something that's already in the client computer's memory, but you want to display all immediately once it's completed loading. Sounds like those "double buffering" in offline media.

What you can do, is:

// Display the loading screen, you can put any animation
$("#loader").fadeIn();
$("#contentPlaceHolder").hide();
// attach the DOM of the contents to placeholder.
$("#contentPlaceHolder").append(CONTENTS);
// .... similar statements follows.
// and finally..
$("#contentPlaceHolder").show();
$("#loader").fadeOut();


xandy
Thank you very much. Just one more question, is there any way I can find the percentage that has been loaded, so when it reaches 100% I can fade out the loader dialogue and in the content
Patrick Gates
Just like what the guys above write, you have to tell us what method you are going to load your content. Is it from AJAX? If AJAX, probably you could only know if the loading is completed rather than the percentage of loading.
xandy
A: 

Regarding the progress bar see this: http://www.caktux.ca/blog/page-loading-progress-jquery

Alexandru Plugaru