Asynchronous Script Loading Callback

前端 未结 3 1225
醉话见心
醉话见心 2020-11-30 02:33

http://jsfiddle.net/JamesKyle/HQDu6/

I\'ve created a short function based on Mathias Bynens Optimization of the Google Analytics asynchr

3条回答
  •  日久生厌
    2020-11-30 03:06

    The other answers works well, but aren't super readable or require Promises. Here is my two cents:

    function loadScript(src, callback) {
        var script = document.createElement('script');
        script.setAttribute('src', src);
        script.addEventListener('load', callback);
        document.head.appendChild(script);
    },
    

提交回复
热议问题