views:

114

answers:

2

jQuery of course requires everything to be inside

$(document).ready = function() {};

similarly, svg-web requires:

window.onsvgload = function() {};

Is there a correct, elegant way to combine these that doesn't introduce any problems?

+1  A: 

You can just bind the functions to run on the appropriate event, like this:

$(function() { //shortcut for $(document).ready(function() {
  //stuff that needs the DOM to be ready
});
$(window).bind('svgload', function() {
  //SVG stuff
});

There's no harm is using both, in fact that's the appropriate usage, always use the event you need, this is no different from document.ready vs window.load when you need images ready, not just the DOM.

If it matters, svgload happens after onload in the browsers that support it as of the time of this answer, not sure if that'll be consistent when other browsers support it though.

Nick Craver
Thanks, but I'm still kind of confused. Where do I put stuff that needs both?
colinmarc
@colinmarc: `svgload` will fire after the DOM is ready, since it's dependent, if it needs both put it in the `.bind('svgload')` call...the SVG elements have to be setup and loaded at that point, so the DOM was ready before then. If it only needs the DOM, stick with `document.ready`.
Nick Craver
this actually gave me some problems - I ended up going with what I posted below - but I'll leave the answer thing because you really helped me understand what was going on
colinmarc
+1  A: 

Finally found this in the 'user manual':

$(document).ready(function() {
    window.addEventListener('SVGLoad', function() {
       // ready to work with SVG now
    }, false);
});
colinmarc