1

I'm trying to create a simple website http://cone.hostei.com/index3.html using jquery. I just want to load html pages into a div using .load() function. The problem is that functions - $('.preloader').hide() and ({ opacity : 1 }), fire before page and its images are fully loaded into a div! how can I fix it?

$(window).load(function () {
 $('li a').click(function () { //event on click
    var toLoad = $(this).attr('href'); //get href of a li element
    $('.load-in').fadeOut('fast', loadContent);
    $('.loader').show(); //show the loader


    function loadContent() {
        $('.load-in').css({ //set opacity 0.3
            opacity: 0.3
        }).load(toLoad, hideLoad); //load html page, then callback
    };


    function hideLoad() {
        $('.load-in').fadeIn('fast',

        function () { //hide preloader and set opacity 1
            $('.loader').fadeOut('fast');
            $('.load-in').animate({
                opacity: 1
            });
        });
    };
    return false;
 });
});
2
  • Your question is unclear. No offence, the code is poorly structured on your page as well. It's very hard to decipher what your intent is. Commented Apr 10, 2013 at 2:47
  • you need to copy the relevant code to the question, not just a link to a web page Commented Apr 10, 2013 at 2:58

2 Answers 2

1

You need to make sure to put your jQuery code inside the $(document).ready() handler. This will make sure your code runs after all elements in the page have been loaded.

See the jQuery API page: jQuery - .ready()

Sign up to request clarification or add additional context in comments.

2 Comments

It's already in a window.load on his page. Look at the code.
That is not the issue at all. Read the question.
0

If you want to detect when the images are fully loaded so that you don't see them appear after the content was shown, you will have to attach a load event handler on all images contained in the .load-in element and wait before all the images are loaded before fading in the element and fading out the loading indicator.

Something like this (based on code from your site):

function loadContent() {
    var $loadIn = $('.load-in');

    $loadIn.css({ opacity : 0.3 }).load(toLoad , function () {
        var $images = $('img', $loadIn),
            imageCount = $images.length,
            loadedCount = 0;

            //wait on images before showing the content
            $images.on('load', function () {
                if (++loadedCount === imageCount) {
                    $images.off('load');
                    hideLoad();
                }
            });

           //make sure the content is still shown if images fails to load
           //or are very slow to load
           setTimeout(function () {
               if (loadedCount !== imageCount) {
                   $images.off('load');
                   hideLoad();
               }
           }, 5000);
    });   
};

However, here's some things you should know (taken from http://api.jquery.com/load-event/):

Caveats of the load event when used with images A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:

  • It doesn't work consistently nor reliably cross-browser

  • It doesn't fire correctly in WebKit if the image src is set to the same src as before

  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache

4 Comments

Thx a lot for your help, I Am a rookie in Jquery, if it woun't trouble you much, could you show how to do it?!
@user2264246, well I just did in my answer, however I have not tested but that's the idea.
@user2264246, so you tried my solution and it's not working? Do you get any errors or it doesn't give the expected result?
Sorry! Maybe I', blind or something, but I can't find ACCEPT button here! I see only ACCEPTED in your message above, that's it! Maybe it's browser issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.