function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dkraundkraun 

running javascript when the body is done loading

I am wondering if there is a way to simulate the functionality of <body onload=''> in a visualforce page.  I have tried setting window.onload to my javascript function, but this gets called before the page actually loads.  It seems like this is due to multiple documents being present in a visualforce page.  I also tried turning off development mode to remove the additional iframe, but there are still multiple documents in the page.  Any ideas?
mshelmanmshelman
This seems like a question that deserves an answer. I've got a google map display problem in IE when the page loads. When the same code is run on a button click after the page is loaded it works just fine -- which indicates that the problem is caused by firing off the javascript before the page is fully loaded.

Mike
dchasmandchasman
This is really more of a general web programming question that something specific to Visualforce. I agree this is something lots of people stumble into. Most popular javascript libraries/toolkits provide some packaged form of using closures to hook and extend onload:

Ext.js provdes Ext.onReady()

onReadyFunction fn, Object scope, boolean override ) : void
Parameters:
  • fn : Function
    The method the event invokes
  • scope : Object
    An object that becomes the scope of the handler
  • override : boolean
    If true, the obj passed in becomes the execution scope of the listener
Returns:
  • void

Dojo provides dojo.addOnLoad()

The lightest weight toolkit of all is most likely Prototype.js and it provides:
Event.observe(window, 'load', function() {
// Do your thing here...
});
If you want just the basic functionality without pulling in an entire library then take a look at this arcticle. All of these libraries basically make use of closures /or event listeners etc.


Message Edited by dchasman on 07-11-2008 10:34 AM