[Problem/Bug]: In WebView2-hosted page, JavaScript window's position properties initially have wrong values
SpoilerJones opened this issue · 3 comments
What happened?
My WinForms app can manage multiple windows with WebView2 controls navigated to pages from the same web application. Sometimes the JavaScript code running in these pages has UI-adjustment code that depends on the size and position of the window. But if that code runs very early in the page lifecycle, the window
object's position properties (screenLeft
/screenX
and screenTop
/screenY
) return 0 instead of the actual position they should. If that logic runs after an asynchronous delay, then the values it sees are accurate.
For example, if the startup code looks like this:
alert(`Window position: (${window.screenLeft}, ${window.screenTop})`);
setTimeout(
()=>alert(`Window position: (${window.screenLeft}, ${window.screenTop})`),
0);
Then the two subsequent alert dialogs will report (0, 0) followed by numbers that match the window's actual position. C# code inspecting the window's Form
's position before running that script agrees that the numbers should not be (0, 0).
I have a couple workarounds I can use for this issue, but they're both pretty ugly, and reorganizing the web app's logic to avoid reading the position too early would be a big undertaking.
(The window's position is also affected by this issue, but I have a separate workaround for it.)
Importance
Moderate. My app's user experience is affected, but still usable.
Runtime Channel
Stable release (WebView2 Runtime)
Runtime Version
131.0.2903.99
SDK Version
2535.41
Framework
Winforms
Operating System
Windows 11
OS Version
22631.4602
Repro steps
- Run the demo app in that folder.
- Click the Launch Second Window button to launch a window in which the script code will run.
- Observe the position values reported in alert dialogs in the new window.
The first alert should say the window is at (0, 0) when it should instead be in the middle of your screen. The second alert will report a position that is more or less in agreement with the window's real position.
Repros in Edge Browser
No, issue does not reproduce in the corresponding Edge version
Regression
Don't know
Last working version (if regression)
No response