views:

93

answers:

1

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

+3  A: 

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.

Dustin Laine
If the browser can understand a partial DOM well enough to display it, I would think it should be able to report on the window's width... I may be wrong of course
Mala
The link was removed in retrospect for privacy reasons as we're not quite ready yet for public exposure, so I'd appreciate it if you'd redact it from your comment. Thank you for the suggestion though! However, google image search seems to accomplish exactly what I'm trying to accomplish, only they do it successfully ;)
Mala
@Mala - It's because the columns are initially static and the rearrangement occurs once the DOM is ready (again, with JS). Perhaps you should play around with a default width for your DIV and when the DOM is ready, you get the DIV adjusted with the JS.
Gert G