I’m trying to implement the Lazy Load plugin into my jQueryMobile site to help speed up loadtimes on pages that contain a ton of images.
If I bypass JQM’s ajax page-loading by going directly to the URL like this: http://hello.com/about, the lazy loading works great. However, if I click-through to the About page from another page, which utilizes JQM’s ajax page-loading, Lazy Load doesn’t load.
The About page has an id of about => <div data-role="page" data-theme="a" id="about">
I’ve tried a number of variations of binding to the pageinit function with no success. My latest attempt is:
$('#about').live('pageinit', function() {
$(window).load(function() {
$("img.lazy").lazyload();
});
});
Any guidance on this would be awesome. Thanks guys.
The
window.loadfunction will fire only once, and you are binding to it after it fires when pages are pulled into the DOM via AJAX. The solution to your issue is pretty easy, run the code when the specific page initializes:This should work just fine since
pageinitwon’t fire until afterdocument.readyfires.Also notice I used
.delegate()instead of.live()since.live()has been depreciated and could be removed from future releases.is the same as:
And for the bonus round, as of jQuery 1.7, both of the above functions actually map to
.on()and you can use.on()in a delegated manor like so:Documentation:
.live(): http://api.jquery.com/live.delegate(): http://api.jquery.com/delegate.on(): http://api.jquery.com/onThe reason your code worked on the first page-view was because the
window.loadevent was firing after you bound to it in thepageinitevent handler, but on subsequent AJAX loads you are binding to thewindow.loadevent even though it won’t fire anymore.