]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/_loader/hostenv_browser.js
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
11 // | if(dojo.isBrowser){ ... }
16 // | if(dojo.isFF > 1){ ... }
21 // | if(dojo.isIE > 6){
28 // | if(dojo.isSafari){ ... }
31 // | if(dojo.isSafari && navigator.userAgent.indexOf("iPhone") != -1){
32 // | // we are iPhone. Note, iPod touch reports "iPod" above and fails this test.
38 // True if the client is a web-browser
40 // isFF: Number | undefined
41 // Version as a Number if client is FireFox. undefined otherwise. Corresponds to
42 // major detected FireFox version (1.5, 2, 3, etc.)
44 // isIE: Number | undefined
45 // Version as a Number if client is MSIE(PC). undefined otherwise. Corresponds to
46 // major detected IE version (6, 7, 8, etc.)
48 // isKhtml: Number | undefined
49 // Version as a Number if client is a KHTML browser. undefined otherwise. Corresponds to major
52 // isWebKit: Number | undefined
53 // Version as a Number if client is a WebKit-derived browser (Konqueror,
54 // Safari, Chrome, etc.). undefined otherwise.
56 // isMozilla: Number | undefined
57 // Version as a Number if client is a Mozilla-based browser (Firefox,
58 // SeaMonkey). undefined otherwise. Corresponds to major detected version.
60 // isOpera: Number | undefined
61 // Version as a Number if client is Opera. undefined otherwise. Corresponds to
62 // major detected version.
64 // isSafari: Number | undefined
65 // Version as a Number if client is Safari or iPhone. undefined otherwise.
67 // isChrome: Number | undefined
68 // Version as a Number if client is Chrome browser. undefined otherwise.
71 // True if the client runs on Mac
75 if(typeof window
!= 'undefined'){
76 dojo
.isBrowser
= true;
77 dojo
._name
= "browser";
80 // attempt to figure out the path to dojo if it isn't set in the config
84 // this is a scope protection closure. We set browser versions and grab
85 // the URL we were loaded from here.
87 // grab the node we were loaded from
88 if(document
&& document
.getElementsByTagName
){
89 var scripts
= document
.getElementsByTagName("script");
90 var rePkg
= /dojo(\.xd)?\.js(\W|$)/i;
91 for(var i
= 0; i
< scripts
.length
; i
++){
92 var src
= scripts
[i
].getAttribute("src");
94 var m
= src
.match(rePkg
);
96 // find out where we came from
97 if(!d
.config
.baseUrl
){
98 d
.config
.baseUrl
= src
.substring(0, m
.index
);
100 // and find out if we need to modify our behavior
101 var cfg
= scripts
[i
].getAttribute("djConfig");
103 var cfgo
= eval("({ "+cfg
+" })");
105 dojo
.config
[x
] = cfgo
[x
];
108 break; // "first Dojo wins"
112 d
.baseUrl
= d
.config
.baseUrl
;
114 // fill in the rendering support information in dojo.render.*
116 var dua
= n
.userAgent
,
118 tv
= parseFloat(dav
);
120 if(dua
.indexOf("Opera") >= 0){ d
.isOpera
= tv
; }
121 if(dua
.indexOf("AdobeAIR") >= 0){ d
.isAIR
= 1; }
122 d
.isKhtml
= (dav
.indexOf("Konqueror") >= 0) ? tv
: 0;
123 d
.isWebKit
= parseFloat(dua
.split("WebKit/")[1]) || undefined;
124 d
.isChrome
= parseFloat(dua
.split("Chrome/")[1]) || undefined;
125 d
.isMac
= dav
.indexOf("Macintosh") >= 0;
127 // safari detection derived from:
128 // http://developer.apple.com/internet/safari/faq.html#anchor2
129 // http://developer.apple.com/internet/safari/uamatrix.html
130 var index
= Math
.max(dav
.indexOf("WebKit"), dav
.indexOf("Safari"), 0);
131 if(index
&& !dojo
.isChrome
){
132 // try to grab the explicit Safari version first. If we don't get
133 // one, look for less than 419.3 as the indication that we're on something
135 d
.isSafari
= parseFloat(dav
.split("Version/")[1]);
136 if(!d
.isSafari
|| parseFloat(dav
.substr(index
+ 7)) <= 419.3){
141 if(dua
.indexOf("Gecko") >= 0 && !d
.isKhtml
&& !d
.isWebKit
){ d
.isMozilla
= d
.isMoz
= tv
; }
143 //We really need to get away from this. Consider a sane isGecko approach for the future.
144 d
.isFF
= parseFloat(dua
.split("Firefox/")[1] || dua
.split("Minefield/")[1]) || undefined;
146 if(document
.all
&& !d
.isOpera
){
147 d
.isIE
= parseFloat(dav
.split("MSIE ")[1]) || undefined;
148 //In cases where the page has an HTTP header or META tag with
149 //X-UA-Compatible, then it is in emulation mode.
150 //Make sure isIE reflects the desired version.
151 //document.documentMode of 5 means quirks mode.
152 //Only switch the value if documentMode's major version
153 //is different from isIE's major version.
154 var mode
= document
.documentMode
;
155 if(mode
&& mode
!= 5 && Math
.floor(d
.isIE
) != mode
){
160 //Workaround to get local file loads of dojo to work on IE 7
161 //by forcing to not use native xhr.
162 if(dojo
.isIE
&& window
.location
.protocol
=== "file:"){
163 dojo
.config
.ieForceActiveXXhr
=true;
166 d
.isQuirks
= document
.compatMode
== "BackCompat";
168 // TODO: is the HTML LANG attribute relevant?
169 d
.locale
= dojo
.config
.locale
|| (d
.isIE
? n
.userLanguage
: n
.language
).toLowerCase();
171 // These are in order of decreasing likelihood; this will change in time.
172 d
._XMLHTTP_PROGIDS
= ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
174 d
._xhrObj = function(){
176 // does the work of portably generating a new XMLHTTPRequest object.
178 if(!dojo
.isIE
|| !dojo
.config
.ieForceActiveXXhr
){
179 try{ http
= new XMLHttpRequest(); }catch(e
){}
182 for(var i
=0; i
<3; ++i
){
183 var progid
= d
._XMLHTTP_PROGIDS
[i
];
185 http
= new ActiveXObject(progid
);
191 d
._XMLHTTP_PROGIDS
= [progid
]; // so faster next time
198 throw new Error("XMLHTTP not available: "+last_e
);
201 return http
; // XMLHTTPRequest instance
204 d
._isDocumentOk = function(http
){
205 var stat
= http
.status
|| 0,
206 lp
= location
.protocol
;
207 return (stat
>= 200 && stat
< 300) || // Boolean
208 stat
== 304 || // allow any 2XX response code
209 stat
== 1223 || // get it out of the cache
210 // Internet Explorer mangled the status code OR we're Titanium/browser chrome/chrome extension requesting a local file
211 (!stat
&& (lp
== "file:" || lp
== "chrome:" || lp
== "chrome-extension:" || lp
== "app:") );
214 //See if base tag is in use.
215 //This is to fix http://trac.dojotoolkit.org/ticket/3973,
216 //but really, we need to find out how to get rid of the dojo._Url reference
217 //below and still have DOH work with the dojo.i18n test following some other
218 //test that uses the test frame to load a document (trac #2757).
219 //Opera still has problems, but perhaps a larger issue of base tag support
220 //with XHR requests (hasBase is true, but the request is still made to document
221 //path, not base path).
222 var owloc
= window
.location
+"";
223 var base
= document
.getElementsByTagName("base");
224 var hasBase
= (base
&& base
.length
> 0);
226 d
._getText = function(/*URI*/ uri
, /*Boolean*/ fail_ok
){
227 // summary: Read the contents of the specified uri and return those contents.
229 // A relative or absolute uri. If absolute, it still must be in
230 // the same "domain" as we are.
232 // Default false. If fail_ok and loading fails, return null
233 // instead of throwing.
234 // returns: The response text. null is returned when there is a
235 // failure and failure is okay (an exception otherwise)
237 // NOTE: must be declared before scope switches ie. this._xhrObj()
238 var http
= d
._xhrObj();
240 if(!hasBase
&& dojo
._Url
){
241 uri
= (new dojo
._Url(owloc
, uri
)).toString();
244 if(d
.config
.cacheBust
){
245 //Make sure we have a string before string methods are used on uri
247 uri
+= (uri
.indexOf("?") == -1 ? "?" : "&") + String(d
.config
.cacheBust
).replace(/\W+/g,"");
250 http
.open('GET', uri
, false);
253 if(!d
._isDocumentOk(http
)){
254 var err
= Error("Unable to load "+uri
+" status:"+ http
.status
);
255 err
.status
= http
.status
;
256 err
.responseText
= http
.responseText
;
260 if(fail_ok
){ return null; } // null
261 // rethrow the exception
264 return http
.responseText
; // String
269 var _handleNodeEvent = function(/*String*/evtName
, /*Function*/fp
){
271 // non-destructively adds the specified function to the node's
273 // evtName: should be in the form "onclick" for "onclick" handlers.
274 // Make sure you pass in the "on" part.
275 var _a
= _w
.attachEvent
|| _w
.addEventListener
;
276 evtName
= _w
.attachEvent
? evtName
: evtName
.substring(2);
277 _a(evtName
, function(){
278 fp
.apply(_w
, arguments
);
283 d
._windowUnloaders
= [];
285 d
.windowUnloaded = function(){
287 // signal fired by impending window destruction. You may use
288 // dojo.addOnWindowUnload() to register a listener for this
289 // event. NOTE: if you wish to dojo.connect() to this method
290 // to perform page/application cleanup, be aware that this
291 // event WILL NOT fire if no handler has been registered with
292 // dojo.addOnWindowUnload. This behavior started in Dojo 1.3.
293 // Previous versions always triggered dojo.windowUnloaded. See
294 // dojo.addOnWindowUnload for more info.
295 var mll
= d
._windowUnloaders
;
302 var _onWindowUnloadAttached
= 0;
303 d
.addOnWindowUnload = function(/*Object?|Function?*/obj
, /*String|Function?*/functionName
){
305 // registers a function to be triggered when window.onunload
308 // The first time that addOnWindowUnload is called Dojo
309 // will register a page listener to trigger your unload
310 // handler with. Note that registering these handlers may
311 // destory "fastback" page caching in browsers that support
312 // it. Be careful trying to modify the DOM or access
313 // JavaScript properties during this phase of page unloading:
314 // they may not always be available. Consider
315 // dojo.addOnUnload() if you need to modify the DOM or do
316 // heavy JavaScript work since it fires at the eqivalent of
317 // the page's "onbeforeunload" event.
319 // | dojo.addOnWindowUnload(functionPointer)
320 // | dojo.addOnWindowUnload(object, "functionName");
321 // | dojo.addOnWindowUnload(object, function(){ /* ... */});
323 d
._onto(d
._windowUnloaders
, obj
, functionName
);
324 if(!_onWindowUnloadAttached
){
325 _onWindowUnloadAttached
= 1;
326 _handleNodeEvent("onunload", d
.windowUnloaded
);
330 var _onUnloadAttached
= 0;
331 d
.addOnUnload = function(/*Object?|Function?*/obj
, /*String|Function?*/functionName
){
333 // registers a function to be triggered when the page unloads.
335 // The first time that addOnUnload is called Dojo will
336 // register a page listener to trigger your unload handler
339 // In a browser enviroment, the functions will be triggered
340 // during the window.onbeforeunload event. Be careful of doing
341 // too much work in an unload handler. onbeforeunload can be
342 // triggered if a link to download a file is clicked, or if
343 // the link is a javascript: link. In these cases, the
344 // onbeforeunload event fires, but the document is not
345 // actually destroyed. So be careful about doing destructive
346 // operations in a dojo.addOnUnload callback.
348 // Further note that calling dojo.addOnUnload will prevent
349 // browsers from using a "fast back" cache to make page
350 // loading via back button instantaneous.
352 // | dojo.addOnUnload(functionPointer)
353 // | dojo.addOnUnload(object, "functionName")
354 // | dojo.addOnUnload(object, function(){ /* ... */});
356 d
._onto(d
._unloaders
, obj
, functionName
);
357 if(!_onUnloadAttached
){
358 _onUnloadAttached
= 1;
359 _handleNodeEvent("onbeforeunload", dojo
.unloaded
);
365 //START DOMContentLoaded
366 dojo
._initFired
= false;
367 dojo
._loadInit = function(e
){
368 if(dojo
._scrollIntervalId
){
369 clearInterval(dojo
._scrollIntervalId
);
370 dojo
._scrollIntervalId
= 0;
373 if(!dojo
._initFired
){
374 dojo
._initFired
= true;
376 //Help out IE to avoid memory leak.
377 if(!dojo
.config
.afterOnLoad
&& window
.detachEvent
){
378 window
.detachEvent("onload", dojo
._loadInit
);
381 if(dojo
._inFlightCount
== 0){
382 dojo
._modulesLoaded();
387 if(!dojo
.config
.afterOnLoad
){
388 if(document
.addEventListener
){
389 //Standards. Hooray! Assumption here that if standards based,
390 //it knows about DOMContentLoaded. It is OK if it does not, the fall through
391 //to window onload should be good enough.
392 document
.addEventListener("DOMContentLoaded", dojo
._loadInit
, false);
393 window
.addEventListener("load", dojo
._loadInit
, false);
394 }else if(window
.attachEvent
){
395 window
.attachEvent("onload", dojo
._loadInit
);
397 //DOMContentLoaded approximation. Diego Perini found this MSDN article
398 //that indicates doScroll is available after DOM ready, so do a setTimeout
399 //to check when it is available.
400 //http://msdn.microsoft.com/en-us/library/ms531426.aspx
401 if(!dojo
.config
.skipIeDomLoaded
&& self
=== self
.top
){
402 dojo
._scrollIntervalId
= setInterval(function (){
404 //When dojo is loaded into an iframe in an IE HTML Application
405 //(HTA), such as in a selenium test, javascript in the iframe
406 //can't see anything outside of it, so self===self.top is true,
407 //but the iframe is not the top window and doScroll will be
408 //available before document.body is set. Test document.body
409 //before trying the doScroll trick
411 document
.documentElement
.doScroll("left");
423 document
.namespaces
.add("v", "urn:schemas-microsoft-com:vml");
424 var vmlElems
= ["*", "group", "roundrect", "oval", "shape", "rect", "imagedata", "path", "textpath", "text"],
425 i
= 0, l
= 1, s
= document
.createStyleSheet();
431 s
.addRule("v\\:" + vmlElems
[i
], "behavior:url(#default#VML); display:inline-block");
436 //END DOMContentLoaded
440 OpenAjax.subscribe("OpenAjax", "onload", function(){
441 if(dojo._inFlightCount == 0){
442 dojo._modulesLoaded();
446 OpenAjax.subscribe("OpenAjax", "onunload", function(){
450 } //if (typeof window != 'undefined')
452 //Register any module paths set up in djConfig. Need to do this
453 //in the hostenvs since hostenv_browser can read djConfig from a
454 //script tag's attribute.
456 var mp
= dojo
.config
["modulePaths"];
458 for(var param
in mp
){
459 dojo
.registerModulePath(param
, mp
[param
]);
464 //Load debug code if necessary.
465 if(dojo
.config
.isDebug
){
466 dojo
.require("dojo._firebug.firebug");
469 if(dojo
.config
.debugAtAllCosts
){
470 dojo
.config
.useXDomain
= true;
471 dojo
.require("dojo._base._loader.loader_xd");
472 dojo
.require("dojo._base._loader.loader_debug");
473 dojo
.require("dojo.i18n");