| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Admin\Form\InitJs; |
| 4 |
|
| 5 |
class ScriptLoader |
| 6 |
{ |
| 7 |
public static function init() |
| 8 |
{ |
| 9 |
return <<<SCRIPT_LOADER |
| 10 |
function scriptLoader(src, integrity, attrs, id, initFunc) { |
| 11 |
const script = document.createElement('script'); |
| 12 |
script.src = src; |
| 13 |
script.id = id; |
| 14 |
if(integrity){ |
| 15 |
script.integrity = integrity; |
| 16 |
script.crossOrigin = 'anonymous'; |
| 17 |
} |
| 18 |
if(attrs){ |
| 19 |
Object.entries(attrs).forEach(function([key, val]){ |
| 20 |
script.setAttribute(key,val); |
| 21 |
}) |
| 22 |
} |
| 23 |
script.onload = function () { |
| 24 |
initFunc && initFunc(); |
| 25 |
}; |
| 26 |
var alreadyExistScriptElm = document.querySelector('script#'+id); |
| 27 |
if(alreadyExistScriptElm){ |
| 28 |
initFunc && initFunc(); |
| 29 |
} else { |
| 30 |
document.body.appendChild(script); |
| 31 |
} |
| 32 |
} |
| 33 |
SCRIPT_LOADER; |
| 34 |
} |
| 35 |
} |
| 36 |
|