On the index.html page of my single-page-application (SPA), I am trying to run the following script:
var js_files = [ "/js/libraries/angular.min.js", "/js/libraries/angular-ui-router.min.js", "/js/libraries/headroom.js", "/js/libraries/angular-material.min.js", "/js/libraries/angular-headroom.js", "/js/main.js", "/js/config.js", "/js/directives.js", "/js/controllers.js", "/js/controllers/site.js", "/js/controllers/application.js", "/js/controllers/auth.js", "/js/controllers/user.js", "/js/controllers/console.js", "/js/controllers/deal.js", "/js/controllers/matching.js", "/js/controllers/helpers.js" ] js_files.forEach(function(file) { var script = document.createElement("script"); script.type = "text/javascript"; script.src = file; script.async = false; document.body.appendChild(script); })
I'm having trouble with how javascript asynchronously loads the script before it is ready to be loaded. The issue manifests when the browser if (hard) refreshed. Here is an image of the error in Chrome Console:
Of course, the error message is misleading and has the potential of creating a rabbit's hole when troubleshooting. The issue has nothing to do with Angular, but rather the dependencies not fully loading before the page is loaded. I can confirm this because similar code involving a much smaller array for files loads successfully when executed (content of the array is different depending on whether the application is executed locally on LOCALHOST:5000 or on the server [Heroku] using our DNS which is parsed using document.location.hostname
).
I searched around StackOverflow and found a few posts that I thought might be helpful in solving my problem, such as the implementation of a window.onload
event or .readyState
property; however, I have not been able to successfully get any of them for my specific use case.
Here is one example: StackOverflow Post
Any help and/or direction from the StackOverflow community would be much much appreciated.