Wednesday, October 15, 2008

Running javascript after window.onload without breaking Sharepoint

If you're anything like me and are working with a lot of Javascript within Sharepoint. You will eventually need to run a script after the page loads. So you could add defer="defer" to your script tag, which sometimes works and sometimes doesn't.

You might be tempted to window.onload = someFunction(). But this will break the way Sharepoint load's it's pages. As Sharepoint also needs to do a lot of things after the page loads.

So the solution is:
window.attachEvent("onload", someFunction);

So instead of overriding the onload event. You are now adding an event to the queue. Simply replace the "someFunction" bit with your function name.

1 comment:

  1. Fantastic! Just what I was looking for. Worked great on my SharePoint 2010 form.

    ReplyDelete