Dynamically load a JavaScript file

后端 未结 28 3355
说谎
说谎 2020-11-22 06:56

How can you reliably and dynamically load a JavaScript file? This will can be used to implement a module or component that when \'initialized\' the component will dynamical

28条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 07:42

    jquery resolved this for me with its .append() function - used this to load the complete jquery ui package

    /*
     * FILENAME : project.library.js
     * USAGE    : loads any javascript library
     */
        var dirPath = "../js/";
        var library = ["functions.js","swfobject.js","jquery.jeditable.mini.js","jquery-ui-1.8.8.custom.min.js","ui/jquery.ui.core.min.js","ui/jquery.ui.widget.min.js","ui/jquery.ui.position.min.js","ui/jquery.ui.button.min.js","ui/jquery.ui.mouse.min.js","ui/jquery.ui.dialog.min.js","ui/jquery.effects.core.min.js","ui/jquery.effects.blind.min.js","ui/jquery.effects.fade.min.js","ui/jquery.effects.slide.min.js","ui/jquery.effects.transfer.min.js"];
    
        for(var script in library){
            $('head').append('');
        }
    

    To Use - in the head of your html/php/etc after you import jquery.js you would just include this one file like so to load in the entirety of your library appending it to the head...

    
    

提交回复
热议问题