util.js, websock.js: sync with noVNC - dynamic script loading.

Sync with noVNC ad29479ca90f9. Use new dynamic script loading
mechanism in util.js.

window.onscriptsload is called when dynamic scripts have loaded
(window.onload fires too early in IE 9).
This commit is contained in:
Joel Martin 2012-10-17 11:52:28 -05:00
parent 388d9573fd
commit 780cb8a9e6
2 changed files with 44 additions and 13 deletions

View File

@ -207,6 +207,45 @@ Util.conf_defaults = function(cfg, api, defaults, arr) {
* Cross-browser routines
*/
// Dynamically load scripts without using document.write()
// Reference: http://unixpapa.com/js/dyna.html
//
// Handles the case where load_scripts is invoked from a script that
// itself is loaded via load_scripts. Once all scripts are loaded the
// window.onscriptsloaded handler is called (if set).
Util.get_include_uri = function() {
return (typeof INCLUDE_URI !== "undefined") ? INCLUDE_URI : "include/";
}
Util._pending_scripts = [];
Util.load_scripts = function(files) {
var head = document.getElementsByTagName('head')[0],
ps = Util._pending_scripts;
for (var f=0; f<files.length; f++) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = Util.get_include_uri() + files[f];
//console.log("loading script: " + Util.get_include_uri() + files[f]);
head.appendChild(script);
ps.push(script);
script.onload = script.onreadystatechange = function (e) {
if (!this.readyState ||
this.readyState == 'complete' ||
this.readyState == 'loaded') {
this.onload = this.onreadystatechange = null;
if (ps.indexOf(this) >= 0) {
//console.log("loaded script: " + this.src);
ps.splice(ps.indexOf(this), 1);
}
// Call window.onscriptsload after last script loads
if (ps.length === 0 && window.onscriptsload) {
window.onscriptsload();
}
}
}
}
}
// Get DOM element position on page
Util.getPosition = function (obj) {
var x = 0, y = 0;

View File

@ -35,23 +35,14 @@ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
Websock_native = false;
(function () {
function get_INCLUDE_URI() {
return (typeof INCLUDE_URI !== "undefined") ?
INCLUDE_URI : "include/";
}
var start = "<script src='" + get_INCLUDE_URI(),
end = "'><\/script>", extra = "";
window.WEB_SOCKET_SWF_LOCATION = get_INCLUDE_URI() +
window.WEB_SOCKET_SWF_LOCATION = Util.get_include_uri() +
"web-socket-js/WebSocketMain.swf";
if (Util.Engine.trident) {
Util.Debug("Forcing uncached load of WebSocketMain.swf");
window.WEB_SOCKET_SWF_LOCATION += "?" + Math.random();
}
extra += start + "web-socket-js/swfobject.js" + end;
extra += start + "web-socket-js/web_socket.js" + end;
document.write(extra);
Util.load_scripts(["web-socket-js/swfobject.js",
"web-socket-js/web_socket.js"]);
}());
}
@ -380,8 +371,9 @@ function close() {
// Override internal functions for testing
// Takes a send function, returns reference to recv function
function testMode(override_send) {
function testMode(override_send, data_mode) {
test_mode = true;
mode = data_mode;
api.send = override_send;
api.close = function () {};
return recv_message;