diff options
author | Sam Potts <sam@potts.es> | 2017-11-09 16:06:49 +1100 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2017-11-09 16:06:49 +1100 |
commit | f9602acf611900d9f9ee27e62e3883fa9394bdee (patch) | |
tree | d74db72cf3e47a49ee9fd00129c03faf71272880 /src/js/utils.js | |
parent | 86a5724bdb84a1dc9e503f5907ad80caf395bf0a (diff) | |
download | plyr-f9602acf611900d9f9ee27e62e3883fa9394bdee.tar.lz plyr-f9602acf611900d9f9ee27e62e3883fa9394bdee.tar.xz plyr-f9602acf611900d9f9ee27e62e3883fa9394bdee.zip |
Callback for loadScript
Diffstat (limited to 'src/js/utils.js')
-rw-r--r-- | src/js/utils.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/js/utils.js b/src/js/utils.js index 02f97d5a..c18fdfbc 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -76,17 +76,26 @@ const utils = { }, // Load an external script - loadScript(url) { + loadScript(url, callback) { // Check script is not already referenced if (document.querySelectorAll(`script[src="${url}"]`).length) { return; } - const tag = document.createElement('script'); - tag.src = url; + // Build the element + const element = document.createElement('script'); + element.src = url; - const firstScriptTag = document.getElementsByTagName('script')[0]; - firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); + // Find first script + const first = document.getElementsByTagName('script')[0]; + + // Bind callback + if (utils.is.function(callback)) { + element.addEventListener('load', event => callback.call(null, event), false); + } + + // Inject + first.parentNode.insertBefore(element, first); }, // Load an external SVG sprite |