Bug 177043 - innerWidth and innerHeight return 0 inside iframe created with document.write
Summary: innerWidth and innerHeight return 0 inside iframe created with document.write
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: Frames (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-16 18:48 PDT by Matthias
Modified: 2017-09-21 11:38 PDT (History)
6 users (show)

See Also:


Attachments
A HTML file showing the bug. (571 bytes, text/html)
2017-09-16 18:48 PDT, Matthias
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matthias 2017-09-16 18:48:59 PDT
Created attachment 321021 [details]
A HTML file showing the bug.

If you write to an iframe, innerWidth and innerHeight are 0 inside the iframe, even after the window "load" event fires. However, even 50ms later the dimensions are correct. This may be because the load event is fired too early, or because these properties are not set correctly. This behavior works as expected on other browsers (the width and height represent the dimensions of the iframe from the outset).
Rather than try to explain the exact circumstances required to reproduce this, I'm going to include the following example which I used to confirm this issue (also attached).

<!DOCTYPE HTML>
<html>
<body>
    <iframe src="about:blank" id="frame"></iframe>
    <script>
        var doc = document.getElementById("frame").contentDocument;
        doc.open();
        doc.write(`<script>
            console.log("start", innerWidth);
            window.addEventListener("load", function () {
                console.log("onload", innerWidth); //Doesn't work. Prints 0
            });
            window.setTimeout(function () {
                console.log("50ms", innerWidth); //Works
            }, 50);
        </scr` + "ipt>");
        doc.close();
    </script>
</body>
</html>
Comment 1 Alexey Proskuryakov 2017-09-18 13:43:39 PDT
I think that this is probably more of an accidental implementation artifact of other browsers. The load event only means that subresources were loaded, not that layout is complete.
Comment 2 Matthias 2017-09-20 18:00:07 PDT
Thanks for that explanation, it helped me understand what's going on better.
I tried the "DOMContentLoaded" event listener, but that didn't work. However, setTimeout(function, 0) did work, because that apparently pushes itself to the end of the command queue, after layout is done.
It still seems counter-intuitive that the load listener fires before the layout is completed in this one case in this one browser, and that there's no official way to ensure layout is finished.
Comment 3 Simon Fraser (smfr) 2017-09-21 11:38:28 PDT
I would expect innerWidth to force a layout.