1/** 2 * Mark/store the script as fully loaded in a global variable. 3 * @param src URL of the script 4 */ 5function markScriptFullyLoaded(src) { 6 if(typeof window.scriptLoadMap !== "undefined") 7 window.scriptLoadMap[src] = true; 8 else { 9 window.scriptLoadMap = []; 10 window.scriptLoadMap[src] = true; 11 } 12} 13 14/** 15 * Returns true if the script has been added to the page 16 * @param src URL of the script 17 */ 18function isScriptAdded(src) { 19 return Boolean(document.querySelector('script[src="' + src + '"]')); 20} 21 22/** 23 * Returns true if the script has been fully loaded 24 * @param src URL of the script 25 */ 26function isScriptFullyLoaded(src) { 27 if(typeof window.scriptLoadMap !== "undefined") 28 return window.scriptLoadMap[src]; 29 else 30 return false; 31} 32 33/** 34 * Load a script. 35 * @param src URL of the script 36 * @param onLoadCallback Callback function when the script is fully loaded 37 * @param onLoadErrorCallback Callback function when the script fails to load 38 * @param retryCount How many times retry laoding the script? (Not implimented here. Logic goes into js.onerror function) 39 */ 40function loadScript(src, onLoadCallback, onLoadErrorCallback, retryCount) { 41 if (!src) return; 42 43 // Check if the script is already loaded 44 if (isScriptAdded(src)) { 45 // If script already loaded successfully, trigger the callback function 46 if (onLoadCallback) onLoadCallback(); 47 48 console.warn("Script already loaded. Skipping: ", src); 49 return; 50 } 51 52 // Loading the script... 53 const js = document.createElement("script"); 54 js.src = src; 55 56 js.onload = () => { 57 markScriptFullyLoaded(src); 58 59 // Optional callback on script load 60 if (onLoadCallback) onLoadCallback(); 61 }; 62 63 js.onerror = () => { 64 // Remove the script node (to be able to try again later) 65 const js2 = document.querySelector('script[src="' + src + '"]'); 66 js2.parentNode.removeChild(js2); 67 68 // Optional callback on script load failure 69 if (onLoadErrorCallback) onLoadErrorCallback(); 70 }; 71 72 document.head.appendChild(js); 73 74}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.