0

I want to show a "Please wait while the page is loading..." message while my page is loading. The page contains a lot of small images which take some time to load. So I don't want to hide the message before these are loaded as well.

I tried to use dojo/domReady! and dojo/ready, but they fire before the images are loaded and thus the page still looks incomplete when I hide the message at this point.

The following code works, but doesn't seem to be the dojo way:

window.onload = hideLoadingMessage();

Registering to window.onload with on doesn't work at all:

require([ 'dojo/_base/window', 'dojo/on' ], function(win, on) {
  on(win, 'onload', hideLoadingMessage);
});

How should I wait for the page to be completely loaded including all the images?

Jan Schatz
  • 334
  • 2
  • 12
  • Possible duplicate of [Execute Javascript When Page Has Fully Loaded](http://stackoverflow.com/questions/1033398/execute-javascript-when-page-has-fully-loaded) – brclz Sep 05 '16 at 10:16
  • @brclz this is partly a duplicate as here Jan Schatz was asking for a dojo way of doing – ben Sep 05 '16 at 11:23

1 Answers1

0

If you want to connect to the window load event, you should not use dojo/_base/window.

Instead just use plain window object. Also use load as event name (and not onload). Using directly window object avoid loading a module that you don't need.

See the below snippets with both solutions (window object and dojo/_base/_window module):

require(['dojo/_base/window', 'dojo/on'], function(baseWin, on) {
  on(window, 'load', function() {
    alert('loaded - using global window object!');
  });
  
  on(baseWin.global, 'load', function() {
    alert('loaded - using dojo window module!');
  });
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
ben
  • 3,558
  • 1
  • 15
  • 28
  • Why do you think the global _window_ variable is better than _dojo/_base/window_'s _global_ variable? Of course I forgot the _.global_ above. – Jan Schatz Sep 06 '16 at 12:44
  • Unless you use managed context (and dealing with frames) `dojo/_base/window::global` is just an alias to `window` object. Using directly `window` object avoid loading a module that you don't need – ben Sep 06 '16 at 13:05