Does Chrome not track onload event in script tags?

I use javascript to dynamically load external javascript files, and in my application I need to know when these scripts are fully loaded into memory before the rest of the process continues. I tried the following code to track the onload event on script tags attached to the head:

var headID = document.getElementsByTagName("head")[0];         
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.onload = scriptLoaded;
newScript.src = '/path/to/file.js';
headID.appendChild(newScript);

Source: http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS

This works fine on FireFox (the latest version of Windows and OS X), however it seems that Chrome (again on Windows and os x) does not properly observe this event, and the callback function is called prematurely.

Any ideas?

+3
source share
2

, jQuery:

var script_tag = $('<script><\/script>');
script_tag.attr('type', 'text/javascript');
script_tag.bind('load', function(e)
{
    console.log('script loaded');
});
script_tag.attr('src', '/path/to/file.js');
$('head')[0].appendChild(script_tag[0]);

, , , , script javascript.

+2

. , , :

newScript.onload = function(){ scriptLoaded; };

, Chrome scriptLoaded, newScript.onload.

0

Source: https://habr.com/ru/post/1787486/


All Articles