inControl
inControl

Reputation: 2324

Javascript load function only works half of the time (Firefox)

When I use the following code

<object id="svg" data="assets/mySVG.svg" type="image/svg+xml">

<script>
$(document).ready(function() {
   document.getElementById('svg').addEventListener("load", function() {
      console.log('test');
   });
});
</script>

It will only load half of the time. Sometimes when I refresh it loads, sometimes it doesn't. Once the SVG document is cached it allways loads. This only happens in firefox, in chrome it loads 100% of the time.

Upvotes: 0

Views: 518

Answers (1)

Rodrigo Calix
Rodrigo Calix

Reputation: 627

Might could be a cache problem, also keep in mind that the event Load occurs after the DOM is ready, and probably the SVG Object loads before because is on the cache, how ever you can try loading it without using jQuery, as i had read before the jQuery ready event takes longer to start because, first the jQuery Library needs to load and probably the SVG is already loaded when the ready event occurs, add a script like this, before the </body>:

<script>
(function () {
    "use strict";
    var init();
    init = function(){
        document.getElementById('svg').addEventListener("load", function() {
            console.log('test');
        });
    }
    init();
})();
</script>

Upvotes: 1

Related Questions