Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - running a function before document.ready... but not too early

I have a page which contains a div which must be resized via JS when a page loads. In order to do that I give it a "default width" of 760px and then run the following code:

function resizeList()
{
    var wlwidth,iwidth,nwidth;
    wlwidth = document.body.clientWidth - 200 - 60;
    iwidth = 320;
    nwidth  = Math.floor(wlwidth / iwidth) * iwidth;
    $('#list').css('width',nwidth);
}

$(document).ready(function(){
    // if we're looking at a list, do the resize-thing, now and on window resize
    if (window.location.pathname.toString().split('/')[1] == "list")
    {
        resizeList();
        window.onresize = resizeList;
    }
});

However, the page can take a while to load as the #list div contains a lot of images. Thus, the div is only expanded to fill the correct width after all of the content has finished loading. I can't just take it out of the $(document).ready function as otherwise it errors out saying document.body is undefined.

Is there a way to resize the #list div before all of its contents have loaded?

Edit
Please see: http://www.google.com/images?q=whatever
They've achieved what I'm trying to do successfully. The list is correctly sized immediately on page load, and is then populated. You can tell they size everything via JS by resizing the window and watching the elements move smoothly. Sadly, google's JS isn't the easiest in the world to read sigh

like image 677
Mala Avatar asked Oct 15 '22 03:10

Mala


People also ask

Can I use jQuery without document ready?

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 Document Object Model (DOM) is ready for JavaScript code to execute.

What is difference between $( document .ready function () vs $( function ()?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.

How do I delay the document ready event?

Approach 1: Using the holdReady() method in the jQuery library and the setTimeout() method. First we set the parameter in the holdReady() method to true to hold the execution of the document. ready() method. Then, a timeout function with an appropriate delay time can be added using the setTimeout() method.

How many time $( document ready (); function we can use in a single document?

Can we add more than one 'document. ready' function in a page? Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.


1 Answers

It is running as soon as it can, $(document).ready is the event that occurs when the dom is completed rendering.

You should add a loading screen onload and then possibly fade it out when after you re-size.

like image 179
Dustin Laine Avatar answered Nov 02 '22 09:11

Dustin Laine