I am working on a website that has a content division that gets loaded dynamically by clicking on links in a navigation bar. The links are bound to jQuery.load(<some HTML page>).
One of my pages that loads into this content window is having a problem. It's a gallery of photos (call it photos.html )that the user can scroll through and it will dynamically load more photos. The way it works is this:
<div id="scrolling_window">
<div id="container">
<ul id="list">
<!-- Content loaded dynamically. -->
</ul>
<div id="loader">
Page Loaded.
</div>
</div>
</div>
The scrolling_window is fixed-height (defined in css) and the container holds the images and grows dynamically to contain the pictures.
In javascript I have a function that calculates when the bottom of the scrolling window is near the bottom of the container. If it gets near the end it loads more images. And so on...
Now, if I view the page for this dynamically loaded gallery on it's own (i.e. manually typing in the URL for the page), everything works fine. However, when I view it the intended way (by clicking on the link and having it loaded into the existing page via a call to jQuery.load) then it doesn't work properly.
The problem seems to be that the math in the javascript is based on the sizes of HTML containers that are defined in CSS. I was able to confirm this by doing some testing and moving around my CSS code references with the following results:
- When explicitly loading
photos.htmlin a browser it only works if the CSS file is loaded BEFORE the javascript file. - When viewing the page by loading the content with jQuery.load it works if the
scrolling_windowis styled like<div id="scrolling_window" style="height:150px"> - When viewing the page by loading the content with jQuery.load it works if the CSS file is loaded on the main page
index.html.
In other words, it seems the CSS file must be loaded before the javascript is executed...
So, my question is... When loading an HTML page into an existing <div> element is it possible to somehow ensure that the CSS referenced on the loaded page is loaded before the javascript is executed?