<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>188499</bug_id>
          
          <creation_ts>2018-08-12 23:53:24 -0700</creation_ts>
          <short_desc>Setting a frame src after appending it to the DOM makes the frame&apos;s load event be fired twice</short_desc>
          <delta_ts>2023-12-20 20:47:28 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Frames</component>
          <version>Safari 11</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=243123</see_also>
    
    <see_also>https://bugs.webkit.org/show_bug.cgi?id=245719</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>BrowserCompat, InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Timothée Pillard">tpillardpro</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>aestes</cc>
    
    <cc>ahmad.saleem792</cc>
    
    <cc>ap</cc>
    
    <cc>beidson</cc>
    
    <cc>cdumez</cc>
    
    <cc>jannis.rautenstrauch</cc>
    
    <cc>rniwa</cc>
    
    <cc>saetia</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1449579</commentid>
    <comment_count>0</comment_count>
    <who name="Timothée Pillard">tpillardpro</who>
    <bug_when>2018-08-12 23:53:24 -0700</bug_when>
    <thetext>Hello,

I am creating this bug in follow-up to https://twitter.com/tpillard/status/1028663435524009984

The following JavaScript code will log the &quot;Loaded&quot; message twice.


```
const root = document.body;
const frame = document.createElement(&apos;iframe&apos;);
const href = &apos;https://bugs.webkit.org&apos;;

frame.onload = (evt) =&gt; console.log(&apos;Loaded&apos;, evt);
root.appendChild(frame);
frame.src = href;
```

Inverting the last two lines (setting the frame&apos;s src before appending it to the DOM) fixes the problem.

Tested and fixed with the following UA string:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15

I&apos;ve originally had some customer complaints on this behavior, one includes the following UA string:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15

While debugging, I noticed similar issues have been reported by users on StackOverflow over the years:
https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice
https://stackoverflow.com/questions/22722046/onload-bug-in-google-chrome-safari
https://stackoverflow.com/questions/25066052/window-onload-called-twice-inside-an-iframe

I could not reproduce this behavior on other browsers I had at my disposal.

Thank you for any response,

--
Tim</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1449580</commentid>
    <comment_count>1</comment_count>
    <who name="Timothée Pillard">tpillardpro</who>
    <bug_when>2018-08-13 00:05:52 -0700</bug_when>
    <thetext>Quick note:

Setting the `load` event handler on the iframe after appending it to the DOM fixes the problem as well.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1451683</commentid>
    <comment_count>2</comment_count>
      <attachid>347527</attachid>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2018-08-20 12:24:29 -0700</bug_when>
    <thetext>Created attachment 347527
Test case</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1451687</commentid>
    <comment_count>3</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2018-08-20 12:28:02 -0700</bug_when>
    <thetext>On interesting, When an iframe is inserted into a document, it loads about:blank which in turn fires a load event. What makes WebKit and Blink&apos;s behavior different from the spec is that we synchronously fire a load event in this case.

See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element

When an iframe element is inserted into a document that has a browsing context, the user agent must create a new browsing context, set the element&apos;s nested browsing context to the newly-created browsing context, and then process the iframe attributes for the &quot;first time&quot;.

https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes

Otherwise, if the element has no src attribute specified, and the user agent is processing the iframe&apos;s attributes for the &quot;first time&quot;
Queue a task to run the iframe load event steps.
The task source for this task is the DOM manipulation task source.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1451688</commentid>
    <comment_count>4</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2018-08-20 12:28:51 -0700</bug_when>
    <thetext>My guess is that making the event load fire async won&apos;t be a Web compatible change at this point but we can try.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1477783</commentid>
    <comment_count>5</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2018-11-12 16:27:33 -0800</bug_when>
    <thetext>&lt;rdar://problem/46008888&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1898018</commentid>
    <comment_count>6</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2022-09-12 14:48:23 -0700</bug_when>
    <thetext>I am able to reproduce this bug in Safari 16 / STP 153 on macOS 12.6 using attached test case and it shows two &quot;Loaded Event&quot; in Console compared to other browsers (Chrome Canary 107 and Firefox Nightly 106), which only shows one.

Just wanted to share updated testing results. Thanks!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1980949</commentid>
    <comment_count>7</comment_count>
    <who name="">jannis.rautenstrauch</who>
    <bug_when>2023-09-28 06:57:04 -0700</bug_when>
    <thetext>Chromium (v117) also fires two load events.
Testing v107 also shows two load events for me.
Firefox only fires one load event.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>347527</attachid>
            <date>2018-08-20 12:24:29 -0700</date>
            <delta_ts>2018-08-20 12:24:29 -0700</delta_ts>
            <desc>Test case</desc>
            <filename>iframe.html</filename>
            <type>text/html</type>
            <size>277</size>
            <attacher name="Ryosuke Niwa">rniwa</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPHNjcmlwdD4KY29uc3Qgcm9vdCA9IGRvY3Vt
ZW50LmJvZHk7CmNvbnN0IGZyYW1lID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgnaWZyYW1lJyk7
CmNvbnN0IGhyZWYgPSAnaHR0cHM6Ly9idWdzLndlYmtpdC5vcmcnOwoKZnJhbWUub25sb2FkID0g
KGV2dCkgPT4gY29uc29sZS5sb2coJ0xvYWRlZCcsIGV2dCk7CnJvb3QuYXBwZW5kQ2hpbGQoZnJh
bWUpOwpmcmFtZS5zcmMgPSBocmVmOwo8L3NjcmlwdD4KPC9ib2R5Pgo8L2h0bWw+Cg==
</data>

          </attachment>
      

    </bug>

</bugzilla>