Javascript stops working after a form submit
Hi,
In my app, I need to prevent page reload after a form submit. So, I get posted data in my ICoreWebView2WebResourceRequestedEventHandler, and then to prevent reload, I set an empty response like this:
wil::com_ptr<ICoreWebView2Environment> environment;
webview2->get_Environment(&environment);
if (environment)
{
wil::com_ptr<ICoreWebView2WebResourceResponse> response;
environment->CreateWebResourceResponse(
nullptr, 404, L"", L"",
&response);
args->put_Response(response.get());
}
The page is then correctly not reloaded, but I can't execute any javascript anymore, be it using ExecuteScript method or the javascript console. Weird thing is, all the scripts that I've tried to execute using ExecuteScript are run later when I trigger a new submit in my page. But javascript is still blocked, all new ExecuteScript attempts are enqueued until the next submit...
Am I missing something or is there a bug in webview2?
Environment: Webview2 1.0.1210.30 Webview2 Runtime 101.0.1210.32 (same issue with previous versions)
MFC app Visual Studio 2019 16.11.13
Hey @cylvoon - Thanks for the bug report, and sorry you're running into this. Does the form submit not show up in NavigationStarting event?
I believe we don't currently have a good way to 'cancel' a web request, and it's possible the way you're doing it is breaking the execution context for the page when trying to run script. We have a similar request as part of the work in #1202, but I'll add this bug to our backlog to dig in further.
Hi @champnic, thanks for you reply. The submit does trigger the NavigationStarting event, but I haven't needed to handle it so far. I successfully get the posted data in the WebResourceRequested event handler by reading the request stream. It's in this event handler I put a null response. It's weird though, that only javascript is broken. I can still submit, and it works (and also process the pending javascripts).