From b85987a6c552c30c60ed5f026a445a255835a923 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 28 Jun 2011 13:03:14 -0700 Subject: [PATCH] supporting the callback after the script is fully loaded See http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer --- .../main/webapp/scripts/hudson-behavior.js | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/war/src/main/webapp/scripts/hudson-behavior.js b/war/src/main/webapp/scripts/hudson-behavior.js index 94c125c5d2..5c5288dae4 100644 --- a/war/src/main/webapp/scripts/hudson-behavior.js +++ b/war/src/main/webapp/scripts/hudson-behavior.js @@ -2064,10 +2064,43 @@ var DragDrop = function(id, sGroup, config) { }); })(); -function loadScript(href) { - var s = document.createElement("script"); - s.setAttribute("src",href); - document.getElementsByTagName("HEAD")[0].appendChild(s); +/** + * Loads the script specified by the URL. + * + * @param href + * The URL of the script to load. + * @param callback + * If specified, this function will be invoked after the script is loaded. + * @see http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer + */ +function loadScript(href,callback) { + var head = document.getElementsByTagName("head")[0] || document.documentElement; + var script = document.createElement("script"); + script.src = href; + + if (callback) { + // Handle Script loading + var done = false; + + // Attach handlers for all browsers + script.onload = script.onreadystatechange = function() { + if ( !done && (!this.readyState || + this.readyState === "loaded" || this.readyState === "complete") ) { + done = true; + callback(); + + // Handle memory leak in IE + script.onload = script.onreadystatechange = null; + if ( head && script.parentNode ) { + head.removeChild( script ); + } + } + }; + } + + // Use insertBefore instead of appendChild to circumvent an IE6 bug. + // This arises when a base node is used (#2709 and #4378). + head.insertBefore( script, head.firstChild ); } var downloadService = { -- GitLab