Load event not fired on Safari when reloading page

自闭症网瘾萝莉.ら 提交于 2019-11-26 11:40:28

问题


I have a simple SVG loaded inside a object tag like the code below. On Safari, the load event is fired just once, when I load the first time the page after opening the browser. All the other times it doesn\'t. I\'m using the load event to initialize some animations with GSAP, so I need to know when the SVG is fully loaded before being able to select the DOM nodes. A quick workaround that seems to work is by using setTimeout instead of attaching to the event, but it seems a bit akward as slower networks could not have load the object in the specified amount of time. I know this event is not really standardized, but I don\'t think I\'m the first person that faced this problem. How would you solve it?

var myElement = document.getElementById(\'my-element\').getElementsByTagName(\'object\')[0];

myElement.addEventListener(\'load\', function () {
    var svgDocument = this.contentDocument;
    var myNode = svgDocument.getElementById(\'my-node\');

    ...
}

回答1:


It sounds more like the problem is that, when the data is cached, the load event fires before you attached the handler.

What you can try is to reset the data attribute once you attached the event :

object.addEventListener('load', onload_handler);
// reset the data attribte so the load event fires again if it was cached
object.data = object.data;


来源:https://stackoverflow.com/questions/34677628/load-event-not-fired-on-safari-when-reloading-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!