unload.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. define(["./kernel", "./lang", "../on"], function(dojo, lang, on){
  2. // module:
  3. // dojo/unload
  4. var win = window;
  5. var unload = {
  6. // summary:
  7. // This module contains the document and window unload detection API.
  8. // This module is deprecated. Use on(window, "unload", func)
  9. // and on(window, "beforeunload", func) instead.
  10. addOnWindowUnload: function(/*Object|Function?*/ obj, /*String|Function?*/ functionName){
  11. // summary:
  12. // Registers a function to be triggered when window.onunload fires.
  13. // Deprecated, use on(window, "unload", lang.hitch(obj, functionName)) instead.
  14. // description:
  15. // The first time that addOnWindowUnload is called Dojo
  16. // will register a page listener to trigger your unload
  17. // handler with. Note that registering these handlers may
  18. // destroy "fastback" page caching in browsers that support
  19. // it. Be careful trying to modify the DOM or access
  20. // JavaScript properties during this phase of page unloading:
  21. // they may not always be available. Consider
  22. // addOnUnload() if you need to modify the DOM or do
  23. // heavy JavaScript work since it fires at the equivalent of
  24. // the page's "onbeforeunload" event.
  25. // example:
  26. // | var afunc = function() {console.log("global function");};
  27. // | require(["dojo/_base/unload"], function(unload) {
  28. // | var foo = {bar: function(){ console.log("bar unloading...");},
  29. // | data: "mydata"};
  30. // | unload.addOnWindowUnload(afunc);
  31. // | unload.addOnWindowUnload(foo, "bar");
  32. // | unload.addOnWindowUnload(foo, function(){console.log("", this.data);});
  33. // | });
  34. if (!dojo.windowUnloaded){
  35. on(win, "unload", (dojo.windowUnloaded = function(){
  36. // summary:
  37. // signal fired by impending window destruction. You may use
  38. // dojo.addOnWindowUnload() to register a listener for this
  39. // event. NOTE: if you wish to dojo.connect() to this method
  40. // to perform page/application cleanup, be aware that this
  41. // event WILL NOT fire if no handler has been registered with
  42. // addOnWindowUnload(). This behavior started in Dojo 1.3.
  43. // Previous versions always triggered windowUnloaded(). See
  44. // addOnWindowUnload for more info.
  45. }));
  46. }
  47. on(win, "unload", lang.hitch(obj, functionName));
  48. },
  49. addOnUnload: function(/*Object?|Function?*/ obj, /*String|Function?*/ functionName){
  50. // summary:
  51. // Registers a function to be triggered when the page unloads.
  52. // Deprecated, use on(window, "beforeunload", lang.hitch(obj, functionName)) instead.
  53. // description:
  54. // The first time that addOnUnload is called Dojo will
  55. // register a page listener to trigger your unload handler
  56. // with.
  57. //
  58. // In a browser environment, the functions will be triggered
  59. // during the window.onbeforeunload event. Be careful of doing
  60. // too much work in an unload handler. onbeforeunload can be
  61. // triggered if a link to download a file is clicked, or if
  62. // the link is a javascript: link. In these cases, the
  63. // onbeforeunload event fires, but the document is not
  64. // actually destroyed. So be careful about doing destructive
  65. // operations in a dojo.addOnUnload callback.
  66. //
  67. // Further note that calling dojo.addOnUnload will prevent
  68. // browsers from using a "fast back" cache to make page
  69. // loading via back button instantaneous.
  70. // example:
  71. // | var afunc = function() {console.log("global function");};
  72. // | require(["dojo/_base/unload"], function(unload) {
  73. // | var foo = {bar: function(){ console.log("bar unloading...");},
  74. // | data: "mydata"};
  75. // | unload.addOnUnload(afunc);
  76. // | unload.addOnUnload(foo, "bar");
  77. // | unload.addOnUnload(foo, function(){console.log("", this.data);});
  78. // | });
  79. on(win, "beforeunload", lang.hitch(obj, functionName));
  80. }
  81. };
  82. dojo.addOnWindowUnload = unload.addOnWindowUnload;
  83. dojo.addOnUnload = unload.addOnUnload;
  84. return unload;
  85. });