Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A17.Helpers.execute_script_tags = function(container) {
- var loaded = false;
- var progress = 0;
- var $container = $(container);
- var current_script = null;
- var head = document.getElementsByTagName('head')[0] || document.body;
- var new_scripts_to_load = $('script[src]', $container);
- var new_scripts_to_eval = $('script:not([src])', $container);
- var existing_scripts = $('script[src]', $(head));
- if(new_scripts_to_load.length) {
- if(progress < new_scripts_to_load.length) startLoad(new_scripts_to_load[progress]);
- }
- function startLoad(target) {
- loaded = false;
- var $target = $(target);
- var src = target.src;
- var matchedScripts = existing_scripts.filter(function() {
- return target.src === src;
- })
- if (matchedScripts.length) {
- matchedScripts.remove();
- }
- var script = document.createElement('script');
- current_script = script;
- if (typeof($target.attr('type')) !== "undefined") {
- script.type = $target.attr('type');
- }
- script.src = $target.attr('src');
- script.onload = handleLoad;
- script.onreadystatechange = handleReadyStateChange;
- script.onerror = handleError;
- head.appendChild(script);
- };
- function handleLoad() {
- if (!loaded) continueLoading();
- }
- function handleReadyStateChange() {
- var state;
- if (!loaded) {
- state = current_script.readyState;
- if (state === "complete") handleLoad();
- }
- }
- function handleError() {
- if (!loaded) continueLoading();
- }
- function continueLoading() {
- loaded = true;
- progress++;
- if(progress < new_scripts_to_load.length) startLoad(new_scripts_to_load[progress]);
- else if(new_scripts_to_eval.length) evalScripts();
- }
- function evalScripts() {
- new_scripts_to_eval.each(function() {
- var target = this;
- var script = document.createElement('script');
- script.text = target.textContent;
- // Place the scripts in the head instead of the body to avoid errors
- // when called from the head in the first place.
- head.appendChild(script);
- // Remove scripts afterwards to avoid unnecessary increased DOM size.
- head.removeChild(script);
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement