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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With