As title says I need lazy javascript loading until when necessary. I know people have asked about this many times on SO and there are a few ways to do so:
- create a script element and append it to head
use jquery's getScript function. this is what I actually used:
$.getScript( some_url ) .done(function( script, textStatus ) { console.log( textStatus ); }) .fail(function( jqxhr, settings, exception ) { $( "div.log" ).text( "Triggered ajaxError handler." ); });
The problem with both approaches is, if the script at *some_url* needs to further load other files, even though some_url finishes loading and executing to me it's still in premature state and fails all my following code. One example is when I try to load Google Maps API at https://maps.googleapis.com/maps/api/js?key=abc&sensor=true at its bottom we can see:
getScript("https://maps.gstatic.com/intl/en_us/mapfiles/api-3/16/3/main.js");
So when my getScript finishes Google's getScript might still be loading. Any thought to solve this problem?