最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is an event fired when an HTML import finishes loading? - Stack Overflow

programmeradmin1浏览0评论

So I am loading a load screen in my app. Then I load my custom elements through HTML imports by making a link element in JavaScript. I need to know when this HTML import has finished loading to display content to the user. Do HTML imports fire an event when the download finishes?

So I am loading a load screen in my app. Then I load my custom elements through HTML imports by making a link element in JavaScript. I need to know when this HTML import has finished loading to display content to the user. Do HTML imports fire an event when the download finishes?

Share Improve this question edited Nov 18, 2018 at 11:57 Supersharp 31.3k11 gold badges102 silver badges147 bronze badges asked Oct 2, 2015 at 5:00 Akheel K MAkheel K M 1701 gold badge2 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

When the HTML import has finished loading, it fires an event called HTMLImportsLoaded. I'd remend you to rely on this event to get a consistant behavior across the different browsers.

window.addEventListener( "HTMLImportsLoaded", function ()
{
    //HTML Imports are loaded
} )

On HTML Imports, read this to understand the difference between onload, HTMLImportsLoaded and WebComponentsReady : http://webponents/polyfills/html-imports/

You have onload event.

<script async>
  function handleLoad(e) {
    console.log('Loaded import: ' + e.target.href);
  }
  function handleError(e) {
    console.log('Error loading import: ' + e.target.href);
  }
</script>

<link rel="import" href="file.html"
      onload="handleLoad(event)" onerror="handleError(event)">

For more on imports, here you go.

If you need to know when all imports are loaded you can use:

window.addEventListener('WebComponentsReady', function() {
    // imports are loaded and elements have been registered
  });
发布评论

评论列表(0)

  1. 暂无评论