window.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright (c) 2004-2012, 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
  5. */
  6. if(!dojo._hasResource["dojo._base.window"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo._base.window"] = true;
  8. dojo.provide("dojo._base.window");
  9. /*=====
  10. dojo.doc = {
  11. // summary:
  12. // Alias for the current document. 'dojo.doc' can be modified
  13. // for temporary context shifting. Also see dojo.withDoc().
  14. // description:
  15. // Refer to dojo.doc rather
  16. // than referring to 'window.document' to ensure your code runs
  17. // correctly in managed contexts.
  18. // example:
  19. // | n.appendChild(dojo.doc.createElement('div'));
  20. }
  21. =====*/
  22. dojo.doc = window["document"] || null;
  23. dojo.body = function(){
  24. // summary:
  25. // Return the body element of the document
  26. // return the body object associated with dojo.doc
  27. // example:
  28. // | 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
  32. };
  33. dojo.setContext = function(/*Object*/globalObject, /*DocumentElement*/globalDocument){
  34. // summary:
  35. // changes the behavior of many core Dojo functions that deal with
  36. // namespace and DOM lookup, changing them to work in a new global
  37. // context (e.g., an iframe). The varibles dojo.global and dojo.doc
  38. // are modified as a result of calling this function and the result of
  39. // `dojo.body()` likewise differs.
  40. dojo.global = globalObject;
  41. dojo.doc = globalDocument;
  42. };
  43. dojo.withGlobal = function( /*Object*/globalObject,
  44. /*Function*/callback,
  45. /*Object?*/thisObject,
  46. /*Array?*/cbArguments){
  47. // summary:
  48. // Invoke callback with globalObject as dojo.global and
  49. // globalObject.document as dojo.doc.
  50. // description:
  51. // Invoke callback with globalObject as dojo.global and
  52. // globalObject.document as dojo.doc. If provided, globalObject
  53. // will be executed in the context of object thisObject
  54. // When callback() returns or throws an error, the dojo.global
  55. // and dojo.doc will be restored to its previous state.
  56. var oldGlob = dojo.global;
  57. try{
  58. dojo.global = globalObject;
  59. return dojo.withDoc.call(null, globalObject.document, callback, thisObject, cbArguments);
  60. }finally{
  61. dojo.global = oldGlob;
  62. }
  63. };
  64. dojo.withDoc = function( /*DocumentElement*/documentObject,
  65. /*Function*/callback,
  66. /*Object?*/thisObject,
  67. /*Array?*/cbArguments){
  68. // summary:
  69. // Invoke callback with documentObject as dojo.doc.
  70. // description:
  71. // Invoke callback with documentObject as dojo.doc. If provided,
  72. // callback will be executed in the context of object thisObject
  73. // When callback() returns or throws an error, the dojo.doc will
  74. // be restored to its previous state.
  75. var oldDoc = dojo.doc,
  76. oldLtr = dojo._bodyLtr,
  77. oldQ = dojo.isQuirks;
  78. try{
  79. dojo.doc = documentObject;
  80. delete dojo._bodyLtr; // uncache
  81. dojo.isQuirks = dojo.doc.compatMode == "BackCompat"; // no need to check for QuirksMode which was Opera 7 only
  82. if(thisObject && typeof callback == "string"){
  83. callback = thisObject[callback];
  84. }
  85. return callback.apply(thisObject, cbArguments || []);
  86. }finally{
  87. dojo.doc = oldDoc;
  88. delete dojo._bodyLtr; // in case it was undefined originally, and set to true/false by the alternate document
  89. if(oldLtr !== undefined){ dojo._bodyLtr = oldLtr; }
  90. dojo.isQuirks = oldQ;
  91. }
  92. };
  93. }