Archive for the ‘javascript’ Category

Safari window.onload timing issue

October 9th, 2009 aaron 2 comments

I ran into some Safari related timing issues at work, that I thought were worthy of a post.  To preface the post I’m using the dojo framework.  On to the post…

I needed to position a flash overlay relative to the absolute postion of a div that will vary in position on subsequent page loads.  So in comes the handy dojo.coords(id) function wrapped in a dojo.addOnLoad.  This works like a charm in firefox 3, ie 6, ie7 and google chrome.  In safari, no dice.  The offsetTop and offsetLeft calculations were completely off (I tried this with my own custom function as well as dojo.coords())  I was finally able to get this to work with using a setTimeout delay (hack).

After researching the issue it seems apple decided to make window.onload fire “before” the page loads.  (Seems counterintuitive to me, but it’s no wonder it’s so fast in benchmarks)  This explains the miscalculation of element positioning on the page.  Waiting for the page to load with a timeout fixes the problem.  The solution is a hack and is obviously not fool proof as in there is no real barrier to execution.  Does anyone in the interweb out there know how to get Safari to fire onload as in when the page actually finishes loading?  Knock knock.