codesite-noreply at google.com
2009-Jun-01 22:33 UTC
[Mapstraction] [mapstraction commit] r14 - Fixed auto-load of mxn scripts in Chrome, for some reason it wasn''t taking script tags adde...
Author: dezfowler Date: Mon Jun 1 15:33:00 2009 New Revision: 14 Modified: trunk/source/mxn.js trunk/tests/domReady.js Log: Fixed auto-load of mxn scripts in Chrome, for some reason it wasn''t taking script tags added with an appendChild into account before firing the DOMContentLoaded event. Amended the domReady.js script to remove some erroneous stuff. Fixes issue 17. Modified: trunk/source/mxn.js =============================================================================--- trunk/source/mxn.js (original) +++ trunk/source/mxn.js Mon Jun 1 15:33:00 2009 @@ -210,13 +210,21 @@ script.type = ''text/javascript''; script.src = src; if (callback) { - var evl = {}; - evl.handleEvent = function(e) { - callback(); - }; - script.addEventListener(''load'' ,evl ,true); + if(script.addEventListener){ + script.addEventListener(''load'', callback, true); + } + else if(script.attachEvent){ + var done = false; + script.attachEvent("onreadystatechange",function(){ + if ( !done && document.readyState === "complete" ) { + done = true; + callback(); + } + }); + } } - document.getElementsByTagName(''head'')[0].appendChild(script); + var h = document.getElementsByTagName(''head'')[0]; + h.appendChild( script ); return; }, @@ -393,15 +401,21 @@ // Auto-load scripts (function() { + // Defaults var providers = ''google,yahoo,microsoft''; var modules = ''core''; var scriptBase; var scripts = document.getElementsByTagName(''script''); + + var writeScriptTag = function(src){ + document.write(''<scr'' + ''ipt type="text/javascript" src="'' + src + ''"></scr'' + ''ipt>''); + }; for (var i = 0; i < scripts.length; i++) { var match = scripts[i].src.replace(/%20/g , '''').match(/^(.*?)mxn\.js(\?\(\[?(.*?)\]?\))?$/); if (match != null) { + scriptBase = match[1]; if (match[3]) { var settings = match[3].split('',[''); @@ -413,8 +427,9 @@ } providers = providers.replace(/ /g, '''').split('',''); modules = modules.replace(/ /g, '''').split('',''); - for (var i = 0; i < modules.length; i++) { - mxn.util.loadScript(scriptBase + ''mxn.'' + modules[i] + ''.js''); - for (var j = 0; j < providers.length; j++) mxn.util.loadScript(scriptBase + ''mxn.'' + providers[j] + ''.'' + modules[i] + ''.js''); + for (var i = 0; i < modules.length; i++) { + writeScriptTag(scriptBase + ''mxn.'' + modules[i] + ''.js''); + for (var j = 0; j < providers.length; j++) writeScriptTag(scriptBase + ''mxn.'' + providers[j] + ''.'' + modules[i] + ''.js''); } + })(); Modified: trunk/tests/domReady.js =============================================================================--- trunk/tests/domReady.js (original) +++ trunk/tests/domReady.js Mon Jun 1 15:33:00 2009 @@ -2,29 +2,24 @@ // Public Domain var WhenDomReady = function(callBack,scope){ - var scope = scope || window; - var args = []; - for (var i=2, len = arguments.length; i < len; ++i) { - args.push(arguments[i]); - }; - if (navigator.userAgent.match(/WebKit/)) - { - (function(){ - if(document.readyState != ''complete'' && document.readyState != ''loaded'') return setTimeout(arguments.callee, 1); - callBack.apply(scope, args); - })(); - } - else if (document.addEventListener) { - document.addEventListener("DOMContentLoaded", function(){callBack.apply(scope, args);}, false); - } - else if (document.all) - { - if(!document.getElementById(''WhenDomReady_element'')) - {document.write("<scr" + "ipt id=\"WhenDomReady_element\" defer=true " + "src=//:><\/scr" + "ipt>"); } - document.getElementById(''WhenDomReady_element'').attachEvent("onreadystatechange",function(){ - if (document.getElementById(''WhenDomReady_element'').readyState=="complete"){ - callBack.apply(scope, args); - } - }); - } + var scope = scope || window; + var args = []; + for (var i=2, len = arguments.length; i < len; ++i) { + args.push(arguments[i]); + }; + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", function(){ + callBack.apply(scope, args); + }, false); + } + else if (document.attachEvent) + { + var done = false; + document.attachEvent("onreadystatechange",function(){ + if ( !done && document.readyState === "complete" ) { + done = true; + callBack.apply(scope, args); + } + }); + } }