matt matt - 7 months ago 35
Javascript Question

Load scripts after page has loaded?

Is it possible to load certain scripts like

<script type="text/javascript" src="somescript.js"></script>

when the rest of the page has loaded?

Imagine I have a few larger script files like this that are not needed when the page is loaded. E.g. I'm using the Google Maps API that is only used when a button is clicked (so not on page load).

Is it possible to load the rest of the page first, before processing all those script tags in my head?


In JQuery you could do this on document ready

$.getScript("somescript.js", function(){
   alert("Script loaded and executed.");