logo

Implementing onDomReady in Domino

So, we've worked out that onLoad is bad and onDomReady is good, but how do we go about using it in Domino?

Chances are you're using a JavaScript library such as jQuery, Mootools or YUI already. In that case you simply need to find out what your library calls its onready event and work out how to use. For example, if you're using mootools, do this:

window.addEvent('domready', function(){
  alert('the dom is ready');
});

If you're using YUI, do this:

function init() { 
  alert('the dom is ready');
}
YAHOO.util.Event.onDOMReady(init);

Which is, effectively, the same as:

YAHOO.util.Event.onDOMReady( function() { 
  alert('the dom is ready');
});

For jQuery it's

$(document).ready(function() {
  alert('the dom is ready');
});

Whichever you use they all seem to function in the same way and use the same hacks to make it all cross browser. To get an understanding of how it works have a look at Dean Edwards' solution, which seems to be what most others are based on. I've copied the code and demo in to an NSF which you can see here and download here.

On a recent project I didn't want the weight of a library to burden the site so I just used Dean's code in a "library.js" file which is included in all pages by default and rarely changes. Also included in all pages is the "functions.js" file where custom code is added and regularly changed. In this functions file we call the onDomReady event which calls the same function in every form in the database. This is how the above demo works.

The trouble with using onLoad in Domino is that you have to add it to every form individually (including view templates). If it changes then you have to edit every single form. With this new method you don't have to make any changes to any forms. Just do it in the one JavaScript resource.

Hope that all makes sense. If not, feel free to ask any questions at all. No matter how daft you might think they are. I still try to write and create content geared towards developers of all knowledge levels. All are welcome here.

Comments

    • avatar
    • John J
    • Tue 5 Jun 2007 07:23 AM

    Hey Jake,

    I get this error when I open up the online demo link.

    Error 404

    HTTP Web Server: Lotus Notes Exception - File does not exist

    Cheers!

    • avatar
    • Jake Howlett
    • Tue 5 Jun 2007 07:31 AM

    Woops. Case-sensitive file name. Fixed now. Thanks John.

    • avatar
    • Paul
    • Tue 5 Jun 2007 10:32 AM

    I still get a 404 error?

    • avatar
    • Jake Howlett
    • Tue 5 Jun 2007 10:35 AM

    Damn it. Right. Try again.

    • avatar
    • Kieren Johnson
    • Tue 5 Jun 2007 10:53 AM

    Great example. I don't think it's possible to make it any clearer than that.

  1. OK - I know that this is only an example, but my problem with this whole onDomReady/onLoad thing is that I don't think you're addressing the real issue here: don't have something that takes THAT long to load in your UI which screws up your usage of onLoad.

    There's nothing keeping you from NOT loading that image as part of the base design and instead using DOM/JS/et al to load it into the UI when the user requests it. Hell, couldn't you even do something like this(?!):

    onLoad="standardonload(); loadbigimg();"

    function loadbigimg() {

    document.getElementById('imganchor').innerHTML = '<img src="bigassimg.jpg" />';

    }

    This approach addresses the issue moreso IMHO in that you can run a lighter UI (which we should ALL be looking to do), have your onLoad event ready much faster, and not have to implement YUI or any other JS Libraries to combat a bloated app UI.

    Bloated Applications != Rich Client Applications

    I like what I'm seeing - please don't get me wrong here - but I think that we should be working with eachother to come up with slimmer, sleeker applications - not hacks to get around bloatware.

    -Chris

    • avatar
    • Jake Howlett
    • Tue 5 Jun 2007 03:09 PM

    Chris. I see your point but not all large imagery is by design. What about a user-driven CMS where -- no matter how many times you tell them -- they keep on uploading raw images from their 10 megapixel camera? Just an example, but it does happen. I've seen it.

    Unless you make your apps text-only then there will always be a lag between when the user can interact with the page and when your code can undo what they just did. etc.

    Jake

    • avatar
    • YoGi
    • Wed 6 Jun 2007 09:33 AM

    I've experienced lots of problems with YUI's onDomReady and IE 6 & 7, for instance, error dialog boxes saying "can't open the URL bla bla". I can't really tell you why.

    Instead, I'm using the following code which so far does work perfectly

    (function() {

    var body = document.getElementsByTagName("body")[0];

    var bodyId = body.id || (body.id = "myBody") ;

    YAHOO.util.Event.onContentReady(bodyId, function() {

    lets_init();

    });

    })();

  2. Chris. Have you ever been to a site that has drop down menus that don't work right away? Use of a DOM ready or DOMContentLoaded event fixes that problem. Before the images, before flash, before all those advertising images you can call your menu, tree structure, whatever initialization functions.

  3. Tanny --

    And how often is that - the delay in having those menus et al "instantly"-accessible - the result of the developer building bulky, clunky, and otherwise bloated web solutions? How many times will they either a) use large active/inactive images for their DHTML menus (not to mention the rest of their site) instead of accomplishing the same with CSS or b) load in a terabyte of script libraries just to get their "cool" woooosh effects?

    I'm not knocking this approach - please understand that, I think it's pretty slick actually - but I am saying that instead of looking to address a symptom, we should look to address the root cause of most of these problems: bad and/or otherwise misguided development.

  4. thanks jake,i wanna know if the prototype class support the onDomReady?

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Tue 5 Jun 2007

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

Elsewhere

Here are the external links posted on the same day.

More links are available in the archive »

More Content