0

In webview2, setting source property to Uri for navigating.

Let's say First URL is opened in webview2 then navigated to another URL. With the back button on Right-click context menu, able to navigate to the first page.

From google search, found there is no direct way to disable back and forward as of now.

In the normal system forms browser, performed an approach like below which is working

added a bool variable(like IsMyNavigationCall), setting it to true whenever just before navigating to some URL Added a check in NavigationStarted event and if it's false(when navigation triggered from actions like back) cancelling the request and resetting the bool variable.

In Webview2, it's not working. The problem is navigation is not cancelled even after setting CoreWebView2NavigationStartingEventArgs.cancel to true.

Is there any way or kind of hack to prevent navigation between the back and forward?

9
  • I think you should do this in javascript, here you can call History.replace when navigating, then there will be only one page in the history, which of course makes it impossible to got back/forward, Commented Mar 8, 2022 at 12:41
  • History Inteface doesn't have Replace member. It has ReplaceState. Should that be used? Commented Mar 8, 2022 at 14:15
  • Sorry for the typo, that was what I meant. Commented Mar 8, 2022 at 14:34
  • 1
    On second thought: May be Location.replace is better: developer.mozilla.org/en-US/docs/Web/API/Location/replace Commented Mar 8, 2022 at 14:37
  • 1
    back button is disabled after changing code from webView.CoreWebView2.Navigate("https://www.google.com"); to await webView.ExecuteScriptAsync("window.location.replace('https://www.google.com')"); Commented Mar 9, 2022 at 2:35

2 Answers 2

1

It seems that you're looking for CoreWebView2.HistoryChanged Event. In order to enable/disable a "Back" button and a "Forward" button when a new URL is navigated to in WebView2, try the following:

Given:

  • WebView2 control: webView21
  • Back button: btnBack
  • Forward button: btnForward
//subscribe to CoreWebView2 events (add event handlers)
webView21.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;

                           ...

private void CoreWebView2_HistoryChanged(object sender, object e)
{
    btnBack.Enabled = webView21.CoreWebView2.CanGoBack;
    btnForward.Enabled = webView21.CoreWebView2.CanGoForward;
}

Sign up to request clarification or add additional context in comments.

3 Comments

I was looking to prevent back /forward navigation in webview2 or prevent the saving of history. CanGoBack and CanGoForward are property with only Get exposed
This is not answer on the question
@23W: This post is more than a year old, but thanks for the feedback.
0

You can disable the context menu and the accelatorkeys via CoreWeView2.Settings https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.1245.22#arebrowseracceleratorkeysenabled

https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.1245.22#aredefaultcontextmenusenabled

Example:

webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;

2 Comments

This Disables all keyboard shortcuts and context menu
It also does not (despite what the msdn reference says) disable the mouse4 and mouse5 hotkeys to navigate back and forth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.