Dynamically load a JavaScript file

后端 未结 28 3004
说谎
说谎 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:41

    I have tweaked some of the above post with working example. Here we can give css and js in same array also.

    $(document).ready(function(){
    
    if (Array.prototype.contains === undefined) {
    Array.prototype.contains = function (obj) {
        var i = this.length;
        while (i--) { if (this[i] === obj) return true; }
        return false;
    };
    };
    
    /* define object that will wrap our logic */
    var jsScriptCssLoader = {
    
    jsExpr : new RegExp( "js$", "i" ),
    cssExpr : new RegExp( "css$", "i" ),
    loadedFiles: [],
    
    loadFile: function (cssJsFileArray) {
        var self = this;
        // remove duplicates with in array
        cssJsFileArray.filter((item,index)=>cssJsFileArray.indexOf(item)==index)
        var loadedFileArray = this.loadedFiles;
        $.each(cssJsFileArray, function( index, url ) {
                // if multiple arrays are loaded the check the uniqueness
                if (loadedFileArray.contains(url)) return;
                if( self.jsExpr.test( url ) ){
                    $.get(url, function(data) {
                        self.addScript(data);
                    });
    
                }else if( self.cssExpr.test( url ) ){
                    $.get(url, function(data) {
                        self.addCss(data);
                    });
                }
    
                self.loadedFiles.push(url);
        });
    
        // don't load twice accross different arrays
    
    },
    addScript: function (code) {
        var oNew = document.createElement("script");
        oNew.type = "text/javascript";
        oNew.textContent = code;
        document.getElementsByTagName("head")[0].appendChild(oNew);
    },
    addCss: function (code) {
        var oNew = document.createElement("style");
        oNew.textContent = code;
        document.getElementsByTagName("head")[0].appendChild(oNew);
    }
    
    };
    
    
    //jsScriptCssLoader.loadFile(["css/1.css","css/2.css","css/3.css"]);
    jsScriptCssLoader.loadFile(["js/common/1.js","js/2.js","js/common/file/fileReader.js"]);
    });
    
    0 讨论(0)
  • 2020-11-22 07:42

    I used a much less complicated version recently with jQuery:

    <script src="scripts/jquery.js"></script>
    <script>
      var js = ["scripts/jquery.dimensions.js", "scripts/shadedborder.js", "scripts/jqmodal.js", "scripts/main.js"];
      var $head = $("head");
      for (var i = 0; i < js.length; i++) {
        $head.append("<script src=\"" + js[i] + "\"></scr" + "ipt>");
      }
    </script>
    

    It worked great in every browser I tested it in: IE6/7, Firefox, Safari, Opera.

    Update: jQuery-less version:

    <script>
      var js = ["scripts/jquery.dimensions.js", "scripts/shadedborder.js", "scripts/jqmodal.js", "scripts/main.js"];
      for (var i = 0, l = js.length; i < l; i++) {
        document.getElementsByTagName("head")[0].innerHTML += ("<script src=\"" + js[i] + "\"></scr" + "ipt>");
      }
    </script>
    
    0 讨论(0)
  • 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('<script type="text/javascript" src="' + dirPath + library[script] + '"></script>');
        }
    

    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...

    <script type="text/javascript" src="project.library.js"></script>
    
    0 讨论(0)
  • 2020-11-22 07:43

    I wrote a simple module that automatizes the job of importing/including module scripts in JavaScript. Give it a try and please spare some feedback! :) For detailed explanation of the code refer to this blog post: http://stamat.wordpress.com/2013/04/12/javascript-require-import-include-modules/

    var _rmod = _rmod || {}; //require module namespace
    _rmod.on_ready_fn_stack = [];
    _rmod.libpath = '';
    _rmod.imported = {};
    _rmod.loading = {
        scripts: {},
        length: 0
    };
    
    _rmod.findScriptPath = function(script_name) {
        var script_elems = document.getElementsByTagName('script');
        for (var i = 0; i < script_elems.length; i++) {
            if (script_elems[i].src.endsWith(script_name)) {
                var href = window.location.href;
                href = href.substring(0, href.lastIndexOf('/'));
                var url = script_elems[i].src.substring(0, script_elems[i].length - script_name.length);
                return url.substring(href.length+1, url.length);
            }
        }
        return '';
    };
    
    _rmod.libpath = _rmod.findScriptPath('script.js'); //Path of your main script used to mark the root directory of your library, any library
    
    
    _rmod.injectScript = function(script_name, uri, callback, prepare) {
    
        if(!prepare)
            prepare(script_name, uri);
    
        var script_elem = document.createElement('script');
        script_elem.type = 'text/javascript';
        script_elem.title = script_name;
        script_elem.src = uri;
        script_elem.async = true;
        script_elem.defer = false;
    
        if(!callback)
            script_elem.onload = function() {
                callback(script_name, uri);
            };
    
        document.getElementsByTagName('head')[0].appendChild(script_elem);
    };
    
    _rmod.requirePrepare = function(script_name, uri) {
        _rmod.loading.scripts[script_name] = uri;
        _rmod.loading.length++;
    };
    
    _rmod.requireCallback = function(script_name, uri) {
        _rmod.loading.length--;
        delete _rmod.loading.scripts[script_name];
        _rmod.imported[script_name] = uri;
    
        if(_rmod.loading.length == 0)
            _rmod.onReady();
    };
    
    _rmod.onReady = function() {
        if (!_rmod.LOADED) {
            for (var i = 0; i < _rmod.on_ready_fn_stack.length; i++){
                _rmod.on_ready_fn_stack[i]();
            });
            _rmod.LOADED = true;
        }
    };
    
    //you can rename based on your liking. I chose require, but it can be called include or anything else that is easy for you to remember or write, except import because it is reserved for future use.
    var require = function(script_name) {
        var np = script_name.split('.');
        if (np[np.length-1] === '*') {
            np.pop();
            np.push('_all');
        }
    
        script_name = np.join('.');
        var uri = _rmod.libpath + np.join('/')+'.js';
        if (!_rmod.loading.scripts.hasOwnProperty(script_name) 
         && !_rmod.imported.hasOwnProperty(script_name)) {
            _rmod.injectScript(script_name, uri, 
                _rmod.requireCallback, 
                    _rmod.requirePrepare);
        }
    };
    
    var ready = function(fn) {
        _rmod.on_ready_fn_stack.push(fn);
    };
    
    // ----- USAGE -----
    
    require('ivar.util.array');
    require('ivar.util.string');
    require('ivar.net.*');
    
    ready(function(){
        //do something when required scripts are loaded
    });
    
    0 讨论(0)
  • 2020-11-22 07:45

    If you want a SYNC script loading, you need to add script text directly to HTML HEAD tag. Adding it as will trigger an ASYNC load. To load script text from external file synchronously, use XHR. Below a quick sample (it is using parts of other answers in this and other posts):

    /*sample requires an additional method for array prototype:*/
    
    if (Array.prototype.contains === undefined) {
    Array.prototype.contains = function (obj) {
        var i = this.length;
        while (i--) { if (this[i] === obj) return true; }
        return false;
    };
    };
    
    /*define object that will wrap our logic*/
    var ScriptLoader = {
    LoadedFiles: [],
    
    LoadFile: function (url) {
        var self = this;
        if (this.LoadedFiles.contains(url)) return;
    
        var xhr = new XMLHttpRequest();
        xhr.onload = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    self.LoadedFiles.push(url);
                    self.AddScript(xhr.responseText);
                } else {
                    if (console) console.error(xhr.statusText);
                }
            }
        };
        xhr.open("GET", url, false);/*last parameter defines if call is async or not*/
        xhr.send(null);
    },
    
    AddScript: function (code) {
        var oNew = document.createElement("script");
        oNew.type = "text/javascript";
        oNew.textContent = code;
        document.getElementsByTagName("head")[0].appendChild(oNew);
    }
    };
    
    /*Load script file. ScriptLoader will check if you try to load a file that has already been loaded (this check might be better, but I'm lazy).*/
    
    ScriptLoader.LoadFile("Scripts/jquery-2.0.1.min.js");
    ScriptLoader.LoadFile("Scripts/jquery-2.0.1.min.js");
    /*this will be executed right after upper lines. It requires jquery to execute. It requires a HTML input with id "tb1"*/
    $(function () { alert($('#tb1').val()); });
    
    0 讨论(0)
  • 2020-11-22 07:45

    Something like this...

    <script>
         $(document).ready(function() {
              $('body').append('<script src="https://maps.googleapis.com/maps/api/js?key=KEY&libraries=places&callback=getCurrentPickupLocation" async defer><\/script>');
         });
    </script>
    
    0 讨论(0)
提交回复
热议问题