简体   繁体   English

页面加载后等待元素加载

[英]Waiting for element load after page has loaded

I have a function that loads a SVG Dom from a file. 我有一个从文件加载SVG Dom的函数。 Currently, it creates an embed element and places it in the document, then waits for it to load with an onload event. 目前,它创建一个embed元素并将其放在文档中,然后等待它加载onload事件。 Apparently, however, onload isn't called for elements placed in the document after the page has loaded. 但是,显然,在页面加载后,不会为放置在文档中的元素onload Is there a way that I can register a function to be called after the element has finished loading? 有没有办法可以在元素加载完成后注册要调用的函数?

This is what I have: 这就是我所拥有的:

function loadSVG(svgFilename, callback)
{
  // Loads data from an svg file
  // loadSVG(
  //           svgFilename, // The path to the file to load
  //           callback     // The function to be called with the
  //                        // SVG document once it loads.
  //        );            
  var embed = document.createElement("embed");
  embed.src = svgFilename;
  embed.onload = function() // Doesn't get called because page has already loaded
  {
    var doc = embed.getSVGDocument();
    document.getElementById("SVGLoader").removeChild(embed);
    callback(doc);
  };
  document.getElementById("SVGLoader").appendChild(embed);
}

I figured out the problem, I was loading the SVG document in a div tag that was hidden with style="display:none" . 我想出了问题,我在一个div标签中加载了SVG文件,该标签隐藏了style="display:none" For some reason, the browser didn't load the embed when it was in this tag. 出于某种原因,浏览器在此标记中没有加载embed When I removed the style attribute, the onload event fired the way I expected it to. 当我删除style属性时,onload事件以我期望的方式触发。

Relevant: How to check if an embedded SVG document is loaded in an html page? 相关: 如何检查嵌入式SVG文档是否加载到html页面?

If you have control over the SVG document, could you not add a script in it that calls a function in the browser window? 如果您可以控制SVG文档,是否可以在其中添加一个在浏览器窗口中调用函数的脚本?

Or, as the answer to that question suggested, polling to see if the SVG document has finished loading. 或者,正如该问题的答案所示,轮询以查看SVG文档是否已完成加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM