Modernizr.load and yepnope.js have both been deprecated. How do we conditionally call javascript files now? Could you give me a example?
This is the javascript that I want to load
var BubbleShoot = window.BubbleShoot || {};
BubbleShoot.Game = (function($){
var Game = function(){
this.init = function(){
$(".but_satart_game").bind("click", startGame);
};
var startGame = function(){
console.log("dentro de startGame en game.js");
//$(".but_satart_game").unbind("click");
//BubbleShoot.ui.hideDialog();
};
};
return Game;
})(jQuery);
And modernizr's code:
Modernizr.load({
load: "_js/game.js",
complete: function(){
$(function(){
var game = new BubbleShoot.Game();
game.init();
})
});
You can dynamically add scripts to a page with
document.createElementand adding it to the DOM with.async = trueand call your game'sinit()function from the script'sloadevent-listener:You can make it more generic by passing the script's URL as a parameter and returning a
Promisefor theloadevent-listener, so pages using this function can have their own custom load logic:Used like so:
...and this is pretty much how the
require()function for loading modules on-demand works, btw.