This article mainly introduces how to dynamically import static resource files such as js and css using javascript. This function is implemented based on callback functions and has some reference value, for more information about how to dynamically import static resource files such as js and css using javascript, see the following example. Share it with you for your reference. The specific implementation method is as follows:
/*** Dynamically import static resource file js/css */var $ import = function () {return function (rId, res, callback) {if (res & 'string' = typeof res) {if (rId) {if ($ ('#' + rId ), $ ('head ')). length> 0) {return ;}// load the resource file var sType = res. substring (res. lastIndexOf ('. ') + 1); // support js/css if (sType & ('js' = sType | 'css' = sType )) {var isScript = (sType = 'js'); var tag = isScript? 'Script': 'link'; var head = document. getElementsByTagName ('head') [0]; // create a node var linkScript = document. createElement (tag); linkScript. type = isScript? 'Text/javascript ': 'text/css'; linkScript. charset = 'utf-8'; if (! IsScript) {linkScript. rel = 'stylesheet ';} isScript? LinkScript. src = res: linkScript. href = res; if (callback & 'function' = typeof callback) {if (linkScript. addEventListener) {linkScript. addEventListener ('load', function () {callback. call () ;}, false) ;}else if (linkScript. attachEvent) {linkScript. attachEvent ('onreadystatechang', function () {var target = window. event. srcElement; if (target. readyState = 'complete') {callback. call () ;}}) ;}} head. appendChild (linkScript );}}};}();
I hope this article will help you design javascript programs.