Best way to reverse the slow loading of JS remote call

I have a remote JS that should appear at the beginning of the document. If the server is slow or unavailable, it is obviously slowing down or preventing the page from loading. I was looking for an easy way to set a limit of 3 seconds (maybe less) so that it would give up and just not load the functionality.

Does anyone have an easy way to do this with Javascript only?

+1


source to share


3 answers


Include the .JS file after the download is complete.

<script type='text/javascript'>
window.onload = function(){
 document.write("<script type='text/javascript' src='http://domain.com/file.js'></script>");
}
</script>

      



Put this in the HEAD of your document, then the file will start loading when the page finishes loading.

+2


source


If this is a problem, split the .js separately and execute the bottom end css functions in and slow ones in onload = "";



0


source


If the window.onload event is called too late for you, then just paste your javascript file directly into the page.

0


source







All Articles