I have created a small script loader function that will iterate over a javascript object populated with names of files in a directory. This is used to dynamically load Mustache templated files to create a page.
I created the javascript object to list the templates in the order I want them output to the page but sometimes when I load the page the elements are not in the right order. I know that in theory that $.each() 'does' iterate in order from top to bottom, but I'm wondering if there is some way I can explicitly force it to do in order to avoid the occasional improperly laid out page.
var loadModules = function(){
var templates = {
1 : 'header',
2 : 'about'
}
$.each(templates, function(key, value) {
$.getScript("js/modules/"+value+".js", function(){
Init();
});
});
}
This is a snippet of the code and from time to time when I load the page you will see the 'about' section appear above the 'header'. Is there any way I can stop this from happening or is it just a 'browser hiccup'?
I created the 'templates' object in order to specify the order of the page elements but it only seems to work most of the time.