1 define("dojo/_base/window", ["./kernel", "../has", "./sniff"], function(dojo, has){
5 // This module provides an API to save/set/restore the global/document scope.
10 // Alias for the current document. 'dojo.doc' can be modified
11 // for temporary context shifting. Also see dojo.withDoc().
13 // Refer to dojo.doc rather
14 // than referring to 'window.document' to ensure your code runs
15 // correctly in managed contexts.
17 // | n.appendChild(dojo.doc.createElement('div'));
20 dojo.doc = this["document"] || null;
22 dojo.body = function(){
24 // Return the body element of the document
25 // return the body object associated with dojo.doc
27 // | dojo.body().appendChild(dojo.doc.createElement('div'));
29 // Note: document.body is not defined for a strict xhtml document
30 // Would like to memoize this, but dojo.doc can change vi dojo.withDoc().
31 return dojo.doc.body || dojo.doc.getElementsByTagName("body")[0]; // Node
34 dojo.setContext = function(/*Object*/globalObject, /*DocumentElement*/globalDocument){
36 // changes the behavior of many core Dojo functions that deal with
37 // namespace and DOM lookup, changing them to work in a new global
38 // context (e.g., an iframe). The varibles dojo.global and dojo.doc
39 // are modified as a result of calling this function and the result of
40 // `dojo.body()` likewise differs.
41 dojo.global = ret.global = globalObject;
42 dojo.doc = ret.doc = globalDocument;
45 dojo.withGlobal = function( /*Object*/globalObject,
47 /*Object?*/thisObject,
48 /*Array?*/cbArguments){
50 // Invoke callback with globalObject as dojo.global and
51 // globalObject.document as dojo.doc.
53 // Invoke callback with globalObject as dojo.global and
54 // globalObject.document as dojo.doc. If provided, globalObject
55 // will be executed in the context of object thisObject
56 // When callback() returns or throws an error, the dojo.global
57 // and dojo.doc will be restored to its previous state.
59 var oldGlob = dojo.global;
61 dojo.global = ret.global = globalObject;
62 return dojo.withDoc.call(null, globalObject.document, callback, thisObject, cbArguments);
64 dojo.global = ret.global = oldGlob;
68 dojo.withDoc = function( /*DocumentElement*/documentObject,
70 /*Object?*/thisObject,
71 /*Array?*/cbArguments){
73 // Invoke callback with documentObject as dojo.doc.
75 // Invoke callback with documentObject as dojo.doc. If provided,
76 // callback will be executed in the context of object thisObject
77 // When callback() returns or throws an error, the dojo.doc will
78 // be restored to its previous state.
80 var oldDoc = dojo.doc,
82 oldIE = dojo.isIE, isIE, mode, pwin;
85 dojo.doc = ret.doc = documentObject;
86 // update dojo.isQuirks and the value of the has feature "quirks"
87 dojo.isQuirks = has.add("quirks", dojo.doc.compatMode == "BackCompat", true, true); // no need to check for QuirksMode which was Opera 7 only
90 if((pwin = documentObject.parentWindow) && pwin.navigator){
91 // re-run IE detection logic and update dojo.isIE / has("ie")
92 // (the only time parentWindow/navigator wouldn't exist is if we were not
93 // passed an actual legitimate document object)
94 isIE = parseFloat(pwin.navigator.appVersion.split("MSIE ")[1]) || undefined;
95 mode = documentObject.documentMode;
96 if(mode && mode != 5 && Math.floor(isIE) != mode){
99 dojo.isIE = has.add("ie", isIE, true, true);
103 if(thisObject && typeof callback == "string"){
104 callback = thisObject[callback];
107 return callback.apply(thisObject, cbArguments || []);
109 dojo.doc = ret.doc = oldDoc;
110 dojo.isQuirks = has.add("quirks", oldQ, true, true);
111 dojo.isIE = has.add("ie", oldIE, true, true);
119 setContext: dojo.setContext,
120 withGlobal: dojo.withGlobal,
121 withDoc: dojo.withDoc