0

I wonder if there is a anyway to remove the WebBrowser Message

are you sure you want to navigate away from this page

When I try to navigate another url, it happens.

Erro picture

I tried this methods

e.Cancel = False

WebBrowser1.Dispose()

WebBrowser1 = New WebBrowser

WebBrowser1.ScriptErrorsSuppressed = True

WebBrowser1.Navigate(New Uri("https://www.google.com"))
Beginner01
  • 21
  • 8

1 Answers1

0

That is coded into the website, so you'd have to inject some Javascript into the page to override the prompt.

Be careful with this, though. This requires overriding the entire window.onbeforeunload event handler, and some pages might have decided to do more than just display a prompt (perhaps save data or something similar).

To start with add a reference to mshtml, this is required to be able to set the contents of a script element (credit to Atanas Korchev):

  1. Right-click your project in the Solution Explorer and press Add Reference....

  2. Select the Browse tab and navigate to C:\Windows\System32

  3. Select mshtml.tlb and press OK.

  4. In the Solution Explorer, expand the References node. If you can't expand it or if it doesn't exist, press the Show All Files button in the top of the Solution Explorer.

  5. Select the mshtml reference, go to the Property Window and make sure Embed Interop Types is set to True.

Now you can use this code:

Private Sub RemoveOnBeforeUnloadPrompt()
    If WebBrowser1.Document IsNot Nothing AndAlso WebBrowser1.Document.Body IsNot Nothing Then
        'Create a <script> tag.
        Dim ScriptElement As HtmlElement = WebBrowser1.Document.CreateElement("script")
        ScriptElement.SetAttribute("type", "text/javascript")

        'Insert code to override the window.onbeforeunload event handler.
        CType(ScriptElement.DomElement, mshtml.IHTMLScriptElement).text = "function __removeOnBeforeUnload() { window.onbeforeunload = function() {}; }"

        'Append script to the web page.
        WebBrowser1.Document.Body.AppendChild(ScriptElement)

        'Run the script.
        WebBrowser1.Document.InvokeScript("__removeOnBeforeUnload")
    End If
End Sub

Then, before you navigate to a new page call:

RemoveOnBeforeUnloadPrompt()
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • it say "InvokeScript"Not Member of WebBrowser1 – Beginner01 Sep 23 '18 at 08:32
  • @Beginner01 : Yeah, I believe you. No need for an image ;). I updated my answer, sorry about that. – Visual Vincent Sep 23 '18 at 08:36
  • i think the problem with the main page of website before navigate to a new page ?! – Beginner01 Sep 23 '18 at 08:42
  • i can send the main page that may help you to figure it out. why it not navigate the next page – Beginner01 Sep 23 '18 at 08:45
  • @Beginner01 : Hmm, okay. Seems there was more to it than just setting `InnerText`. I'll update my answer in a bit. – Visual Vincent Sep 23 '18 at 08:51
  • @Beginner01 : I don't know what that has got to do with anything, but anyway I updated my answer. – Visual Vincent Sep 23 '18 at 09:03
  • unfortunately it has some error too. okey i will do my best to know how to solve it, big thanks for your helping. – Beginner01 Sep 23 '18 at 09:09
  • @Beginner01 : I don't understand, you're gonna do your best trying to know how to solve what? – Visual Vincent Sep 23 '18 at 09:11
  • i will try to negative another link, cause your currently method not work with me unfortunately. – Beginner01 Sep 23 '18 at 09:25
  • @Beginner01 : Not even after my update? That's strange... I'll have to test this later when I get the chance (I'm currently on my phone). – Visual Vincent Sep 23 '18 at 09:28
  • thanks it not a word for you, cause it little to say, you are awesome , also i will try hard to figure it out with you – Beginner01 Sep 23 '18 at 09:31
  • No more Talk, you definitely number 1 , Big thanks, it worked perfectly without any error. and it successfully navigate the next page. – Beginner01 Sep 23 '18 at 11:05
  • @Beginner01 : Always glad I can help! Though I'm very confused right now... So you didn't see my update before, or...? :) – Visual Vincent Sep 23 '18 at 11:17
  • i Saw your new update since15 min ago, and i already made it from your update. – Beginner01 Sep 23 '18 at 11:21
  • that's why i mark your answered as that correct answer, Because you are the best, and your method it work , i import mshtml.tlb , and edit code as you said , now all work good. – Beginner01 Sep 23 '18 at 11:24
  • @Beginner01 : Okay, glad to hear it worked! Good luck with your future endeavours! -- **EDIT:** I just noticed that my phone had done an autocorrect: `Embed Interpol Types` should've been `Embed Interop Types`. I corrected this in my answer as well. – Visual Vincent Sep 23 '18 at 11:29
  • yeh i notice that when you said that you use your android., also im big fan of you, you are almost my Engine search on stackoverflow thread you answered almost my search question's. – Beginner01 Sep 23 '18 at 11:39
  • @Beginner01 : I have an iPhone! _\*ghasp\*_ - Hehe, as I said, I'm glad I can help! – Visual Vincent Sep 23 '18 at 12:04