Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to modify/print a pop-up window with programmatically created content doesn't work if an event handler is attached to NewWindowRequested #409

Closed
RichardSteele opened this issue Aug 27, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@RichardSteele
Copy link

RichardSteele commented Aug 27, 2020

This is another follow-up to issues #134 and #293. The crash is gone now, but the original use case doesn't work.

Following code works as intended if you let WebView2 handle the created pop-up window. If you attach an event handler to NewWindowRequested to create the pop-up window yourself, it doesn't show "Hello World" and doesn't get printed.

var popup = window.open();
popup.document.write('Hello World');
popup.print();

AB#28898656

@champnic
Copy link
Member

I've filed an internal bug to investigate. Third time's the charm! We'll verify your full scenario this time.

@cgeier
Copy link

cgeier commented Sep 8, 2020

Note sure of your use, so I'm not sure if it will meet your needs or not, but I created a small demo project that may work for you.

WebView2TestNewWindowRequested.zip

To use the demo:

  • Open solution in Visual Studio
  • Select Debug
  • Select Start Debugging
  • Click Open Popup Window

*Note: In the demo project, there's no need to enter anything in the URL address bar--the HTML is automatically loaded from the "index.html" file. It's there in case one wants to navigate to a URL, instead of using the HTML that's loaded by default.

Here's an overview:

index.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Popup Window Test</title>

    <script>
        function popupHandler()
        {
            var popup = window.open();
	    popup.document.write('Hello World');
	    popup.print();
        }
    </script>
</head>

<body>
    <p>
        <a href="javascript: popupHandler()">Open Popup Window</a>
    </p>
</body>
</html>

WebView2 control name: webView21

  • Subscribe to CoreWebView2Ready event (webView21_CoreWebView2Ready)
  • Initialize CoreWebView2 using await webView21.EnsureCoreWebView2Async(null);
private void webView21_CoreWebView2Ready(object sender, EventArgs e)
{
    //subscribe to event(s) (add event handler)
    webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
    //e.Handled = true; // <-- marks the event as handled, which bypasses/ignores the other event handler(s) in index.html (javascript)

}

@kumarmohannsn
Copy link

When I opened a child window using NewWindowRequested and Marking the event handled as true. child window got opened, then when I click the cancel button in the child window the child window is not closing. and when I inspect the child window javascript it display error as “javascript must not be allowed to close a window that was not opened by that same javascript". it seems in child window unable to get the parent reference . can anyone help me to solve the issue. Note: I tried in latest version of Webview2 (wpf and winform)

@champnic
Copy link
Member

This is fixed in 1.0.781-prerelease SDK package. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants