On my website, I catch clicks on links to load the content of that page dynamically using AJAX. This new content may require some CSS file that isn't loaded yet. Setting the content of the target element to the response HTML will automatically load the required CSS (the <link>
tags are there), but the content will look messed up for a brief period until the CSS gets loaded.
Other than loading all CSS files in advance, what can I do to prevent this? For completeness sake, here is my code:
$(document).ready(function() {
$('a').live('click', function(event) {
if (this.target || this.rel == 'ignore' || this.rel == 'fancybox') {
return true;
}
if (/^javascript:/.test(this.href)) {
return true;
}
if (this.href.indexOf('#') >= 0) {
return false;
}
if (!this.href) {
return true;
}
loadDynamicPage(this.href);
event.preventDefault();
return false;
});
});
var currentLoader = null;
function loadDynamicPage(url) {
if (currentLoader) {
currentLoader.abort();
currentLoader = null;
}
$('#backButton').addClass('loaderBG');
currentLoader = $.ajax({
context: $('#army_content'),
dataType: 'text',
url: url + '&format=raw',
success: function(data) {
currentLoader = null;
$('#backButton').removeClass('loaderBG');
clearRoomIntervals();
$(this).html(data).find('[title]').tooltip({
showURL: false,
track: false,
delay: 300
});
$(document).trigger('dynamicLoad');
}
});
}