JavaScript to detect when external javascripts are loading

Is there a way (event listener or otherwise) to detect when a particular external javascript is about to load / is loading / has finished loading?

In otherwords, does the browser fire an event when it's about to load, is loading, and/or has finished loading a particular external script?

For my purposes it's not enough to simply check to see if a known object exists or anything like that. Instead, I need something that will detect a JS file is loading/loaded regardless of the contents of the JS file.



The following example works in Chrome. It attaches an handler on the onload event of the head tag and then adds an external javascript file. When the file is loaded, the event is captured and an alert appears.

window.onload = function() {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script"); 

    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", "");

    head.addEventListener("load", function(event) {
        if ( === "SCRIPT")
            alert("Script loaded: " +"src"));
    }, true);


Since all browsers blocks the "UI thread" when processing <script> tags, you can rely that pre-existing tags are loaded.

If you are loading a script dynamically, you can listen to the load event of the <script> tag.

function loadScript(src, callback) {
    var script  = document.createElement("script");
    script.setAttribute("src", src);
    script.addEventListener("load", callback);

loadScript("", function(){
    alert("loading is done");

I wrote a script for those who wants to call a function after all external files (dynamically added) are loaded. It goes like this:

var file = [];
var loaded = [];
var head = document.getElementsByTagName('head')[0];

var fileOnLoad =
// Pass the arrays to your function
(function(file, loaded){ return function(){
  // Print the number of files loaded
  document.getElementById("demo").innerHTML +=
   "<br>"+loaded.length+" files loaded";
  if(file.length == loaded.length){
    alert("All files are loaded!");
}})(file, loaded);

////                                        ////
////   Add the external files dynamically   ////
////                                        ////

file[0] = document.createElement('script');
file[0].src =
file[0].onload = fileOnLoad;

file[1] = document.createElement('script');
file[1].src =
file[1].onload = fileOnLoad;

file[2] = document.createElement('script');
file[2].src =
file[2].onload = fileOnLoad;

file[3] = document.createElement('link');
file[3].rel = "stylesheet";
file[3].href =
file[3].onload = fileOnLoad;

file[4] = document.createElement('link');
file[4].rel = "stylesheet";
file[4].href =
file[4].onload = fileOnLoad;
<div id="demo">0 file loaded</div>

Hope it helps!


If you can use jQuery, try $.getScript(). Docs here.


<script onload> will fire when a script is finished loading.

You will not be able to do something like:

<script src="/foo.js"></script>
<script src="/bar.js"></script>
function alertonload(src){
   console.log(src+' is loaded');
scripts = document.getElementsByTagName('script');
for(var i=0; i<scripts.length; i++){
    scripts[i].onload = function(){ alertonload(scripts[i].src); };

This is pure conjecture and speculation; I have not tried it and there's probably better ways to write it, but this will not do what you're looking to do. EDIT: The scripts are loaded as the browser sees them, not after the fact. They will be loaded before this occurs.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.