load event not firing on svg with svgweb

2019-09-05 04:21发布

问题:

I try to do some stuff after a SVG (referenced by an <object>:)

<!--[if !IE]>-->
<object data="file.svg" type="image/svg+xml" id="image-1" width="760" height="730" > <!--<![endif]-->
<!--[if lt IE 9]>
<object src="file.svg" classid="image/svg+xml" width="200" height="200" id="image-1" width="760" height="730"> <![endif]-->
<!--[if gte IE 9]>
<object data="file.svg" type="image/svg+xml" id="image-1" width="760" height="730">  
<![endif]-->
</object>

is loaded:

a = document.getElementById("image-1");
a.addEventListener("load",function(){
   //some stuff
},false);

This works fine in browser with native SVG support. Yet when the SVG is served with svgweb's flash support, I can't get a load event fired. Did I mess something up or this to be expected?

What can I do to fire an event when the flash fallback is ready? I need this to dynamically hide/show <path>s.

回答1:

Have you tried listening for the 'SVGLoad' event instead?

For how, see the user manual for SVGWeb.



回答2:

I've had trouble getting SVGWeb load events to fire consistently in different browsers with embedded SVG files. I've found this method to work everywhere and with jQuery:

At the END of your SVG file before the </svg> tag:

<script type="text/javascript"><![CDATA[
    window.parent.SVGInit();
]]></script>

Then in your HTML file ABOVE the SVG embed:

<script>
    function SVGInit() {
        $(document).ready(function(){
            // SVG is loaded and ready.
        });             
    }
</script>


回答3:

Which version of svgWeb are you using? Try updating to Lurker Above. Previous versions might override jquery's load handler. Also, try using this syntax :

mysvg.load = mysvg.onsvgload = function(){
    //Handler
}