This article describes how JS dynamically inserts and executes callback functions immediately. Share it for your reference, as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Unt titled document</title></head><body><div id="loading">Loading...</div><mce:script type="text/JavaScript"><!--function loadJs(id,url,callback){ var script = document.createElement('script'); script.type = 'text/javascript'; script.src = url; script.id = id; script.onload = script.onreadystatechange = function(){ alert(script.readyState); if(script.readyState && script.readyState != 'loaded' && script.readyState != 'complete') return ; script.onreadystatechange = script.onload = null if(callback) callback(); } document.body.appendChild(script);}loadJs('jQuery','jquery.js',function(){$('#loading').html('jquery.js loaded')})// --></mce:script></body></html>Dynamically inserting js files plays a very important role in improving page loading speed and cross-domain issues. The above is a simple example.
Onreadystatechange is supported on ie, but not onload
Firefox supports onload, not onreadystatechange
The above ie does not necessarily have to be loaded or triggered by complete, or both will be triggered, so use or to judge.
It should be noted that the case forms of script.onreadystatechange and script.readyState may cause difficult-to-discover errors if case is not case sensitive.
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.