Ways to add javascript files dynamically in a page

后端 未结 9 1623
盖世英雄少女心
盖世英雄少女心 2020-11-28 09:16

I have seen Scriptaculous.js file to include its required javascript files dynamically. Is there any better approach to include javascript dynamically.

For example,

9条回答
  •  [愿得一人]
    2020-11-28 09:38

    there are many different ways, but the way Google loads additional scripts is like this:

    function getScript(src) {
        document.write('<' + 'script src="' + src + '"' +
                       ' type="text/javascript"><' + '/script>');
    }
    

    This is taken directly from Google maps loader.

    Writing a script tag directly to the document is the simplest and most efficient way.

提交回复
热议问题