I am trying load content from another url using pushstate/html5… The content I am trying to load has a big background image which takes time to load…
So when it animates fast full image seems to come up and slide in but then image reloads again…
I tried using
image preloader/
$(document).ready()
they all break the script and can’t quite seem to figure out how to incoprate it here
function goTo(href) {
var left = $(window).width();
$('#id').css("left", left);
$.ajax({
url: href,
success: function (data) {
var content = $(data).find('#id').html();
// Windows Load Function
$(window).load(function () {
$("#id").html(content).animate({
left: '0',
}, 'fast');
var title = $('#id').find('h1').text();
$('head').find('title').text(title);
});
}
});
}
// check for support before we move ahead
if (typeof history.pushState !== "undefined") {
var historyCount = 0;
// On click of link Load content and animate
$('.access a').live('click', function () {
var href = $(this).attr('href');
goTo(href);
history.pushState(null, null, href);
return false;
});
window.onpopstate = function () {
if (historyCount) {
goTo(document.location);
}
historyCount = historyCount + 1;
};
}
MonteCristo,
I think this is best addressed with Deferreds.
In the code below, a Deferred object is passed to
goTo()as a second argument,animator. This Deferred is resolved insidegoTo()when all images with classimgSelectorhave sussessully loaded. On resolution ofanimatorthe animation fires and the page’s title is changed.Creating
animatorexternally and passing it togoTo()allows this Deferred to be rejected from the scope wheregoTo()is called. This feature is included to inhibit the animation should another click or popstate event occur before the animation fires.To make this work, you will need to serve each content page with a dummy off-screen foreground
<img>with the samesrcas the problematic large background image andclass="critical". This is the easiest way to trigger an event when the background image loads.untested
You may well need to debug and/or modify the code to get exactly what you want.