0

I'm working on a JavaScript library which needs to give the user the ability to run some code on load. Of course, I'm familiar with window.onload, and things like $(function() {}); with jQuery. But I don't want to be dependent on another library, and I want this particular function (along with the rest of the lib, of course) to be cross-browser.

So is there an accepted way of attaching to the onload function without overriding another library's load functionality (or having mine overwritten if they include another lib after mine)?

Joel Martinez
  • 44,051
  • 25
  • 123
  • 182
  • Yes. "See how jQuery [or framework X] does it". Sadly, this is still not entirely cross-browser (yay for different event models!). In any case, this is a "solved problem" and source, including work-abouts, is readily available. –  Dec 12 '12 at 01:35
  • 2
    A common strategy is to put onload scripts in a script element just before the closing body tag. No need for `window.onload` or `DOMReady` or whatever. Works everywhere. – RobG Dec 12 '12 at 01:35
  • @RobG "Usually works". Sometimes the point of handling an event [later] than inline script elements is because of dependencies [but not the kind in the title] on other component .. –  Dec 12 '12 at 01:39
  • @pst, I was hoping to avoid traipsing through a huge codebase such as jQuery. If it's a "solved problem", then surely someone has written about it somewhere? – Joel Martinez Dec 12 '12 at 01:40
  • @JoelMartinez It's not a huge library in question: it's a specific task. Very little traipsing is required, especially with a directed search. –  Dec 12 '12 at 01:41
  • 2
    Expose an initialization/onload method and require it be called by the consumer on load. In other words, push the issue off to whomever is using the library and will likely have a convenient method like that offered by jQuery. It is a solved problem but it isn't simple: [javascript onload without jQuery](https://www.google.com/search?q=javascript+onload+without+jquery) (the first hit has the source pulled from a version of jQuery). – Cymen Dec 12 '12 at 01:41
  • @Cymen ahh ... that's perfect (http://stackoverflow.com/a/800010/5416), I looked through StackOverflow for something like this but didn't come across it; though I'll mention that your comment doesn't directly answer the question, the link led me to the question. I'm sure I can rewrite a version of that initialization that works for me. If you make this an actual answer rather than a question, I'll accept it :) – Joel Martinez Dec 12 '12 at 01:46
  • @JoelMartinez Done. I'm happy to edit it to something more appropriate if you found one of the links particularly helpful. – Cymen Dec 12 '12 at 02:02

1 Answers1

1

Expose an initialization/onload method and require it be called by the consumer on load. In other words, push the issue off to whomever is using the library and will likely have a convenient method like that offered by jQuery.

This is a solved problem but it isn't simple: the answer to "$(document).ready equivalent without jQuery" has the code of how jQuery does this.

More hits: javascript onload without jQuery

Community
  • 1
  • 1
Cymen
  • 12,733
  • 4
  • 47
  • 68
  • Thanks, yeah it was the one you mentioned (http://stackoverflow.com/a/800010/5416) with the details of how jQuery does it. – Joel Martinez Dec 12 '12 at 02:06