代码之家  ›  专栏  ›  技术社区  ›  Bill the Lizard

哪些html标记支持onload/onerror javascript事件属性?

  •  21
  • Bill the Lizard  · 技术社区  · 16 年前

    onload ,如下所示:

    <body onload="alert('Hello, World!');">
    ...
    </body>
    

    触发加载事件的所有html元素是什么?(因此执行onload属性中提供的javascript)

    例如 img 是这样一个标记,它将执行 属性何时 some.png

    <img onload="someImgLoaded()" src="some.png" />
    
    5 回复  |  直到 11 年前
        1
  •  18
  •   Alexander O'Mara    10 年前

    以下HTML标记支持“onload”:

    <body>, <frame>, <frameset>, <iframe>, <img>, <link>, <script>

    以及以下Javascript对象:

    image, layer, window

        2
  •  9
  •   Devin Rhode    11 年前

    下面是更全面的元素列表,这些元素在请求的资源完成下载时触发加载事件:

    body # (just fires a load event, doesn't make requests itself)
    img
    image
    link
    iframe
    frameset
    frame
    script
    embed
    object
    video ?
      source
      track
    audio ?
      source
    svg
    <input type="image" src="submit.gif" alt="Submit">
    <object width="400" height="400" data="helloworld.swf"></object>
    <map name="planetmap">
      <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
    webgl?
    

    对于大多数覆盖率,最好考虑引用URL的所有HTML元素将导致请求并引发一个请求。 load error 事件的请求成功或失败时。所以,基本上,任何元素 src href 属性,但这些标记除外:

    a
    # What else? Not sure off hand..
    

    body 标签,因为讽刺的是它没有 href 属性

    var tagsToIgnore = ['a'];
    
    ['src', 'href'].forEach(function(attr) {
      console.log('====' + attr + '====');
      [].slice.call(document.querySelectorAll('*[' + attr + ']')).forEach(function(el){
        if (!~tagsToIgnore.indexOf(el.tagName.toLowerCase())) {
          console.log(el.tagName);
        }
      });
    });
    console.log('body # :trollface:');
    

    此外,使用“everything with src or href”方法,您可以忽略通常具有src或href属性的不相关标记或其他标记,但并不总是如此。

    其他可能导致网络故障的因素:

    onload onerror 属性对于跟踪您的用户是否具有活动的internet连接非常有用,这是我正在尝试使用my library check-online.js解决的问题: http://github.com/devinrhode2/check-online

        3
  •  2
  •   Bill the Lizard    12 年前

    onload 是特定于 body , frame , iframe img link script 身体

        4
  •  1
  •   isherwood    9 年前

    许多元素都有onload事件。你可以找到他们 here

    但是如果您想测试DOM的加载,那么最好使用 window.onload . 还建议您将javascript代码与HTML标记分开。

        5
  •  0
  •   Bill the Lizard    12 年前

    this page ,你可以使用 onload 与: <body> , <frame> <frameset> , <iframe> , <img> , <link> <script>