שתף באמצעות


WebBrowser/IE - How do I use "window.onbeforeunload = null" to turn off (Are you sure you want to leave this page)

Question

Wednesday, November 2, 2016 5:11 PM

Hi, 

How do I use "window.onbeforeunload = null" to turn off (Are you sure you want to leave this page) cause I already tried to follow this link and that link but still unable to accomplish it.

All replies (16)

Wednesday, November 2, 2016 9:56 PM

You are not talking VB.NET here,  and what you are really talking about is JavaScript. I suspect that in order to get the functionality to work, you may need to do some kind of a OnDocumentReady JavaScript function and tie it to an event.

The forum you need to post to is the JavaScript forum in the ASP.NET forum.

http://forums.asp.net/

Why are you not using ASP.NET to do this? Why are using the Webbrowser control?

 


Thursday, November 3, 2016 3:01 AM

Hi LetMeCode,

Thank you for posting in MSDN Forum.

Based on your description and the link, I tried to use "window.onbeforeunload = null; " to turn off the dialog (Are you sure you want to leave this page). I made it! So I think maybe your code has problems. And I used the code below, please check and refer that if it helps.

  • Create a html page in your project.
<!DOCTYPE html>
<html>
<head>
    <script>
  function closeIt()
  {
    return "Any string value here forces a dialog box to \n" +
         "appear before closing the window.";
  }
  window.onbeforeunload = null;
    </script>

</head>
<body>
    <a href="http://www.microsoft.com">
        Click here to navigate to
        www.microsoft.com
    </a>
</body>
</html>
  •  Create a Windows Forms application in Visual Studio, then drag a WebBrowser control to the default form, and then set its Url property.
Imports mshtml

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://localhost:59739/InjectJS.html")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim headElement As HtmlElement = WebBrowser1.Document.GetElementsByTagName("head")(0)
        Dim scriptElement As HtmlElement = WebBrowser1.Document.CreateElement("script")
        headElement.AppendChild(scriptElement)
    End Sub
End Class

If you have something else regarding this issue, please feel free to contact us.

Best Regards,

Neda Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Thursday, November 3, 2016 1:19 PM

Thanks Neda Zhang for your reply.

I tried your code, but still not working. I'm using this web-site to navigate.

I did follow every single step but without success.

By the way, looking at your code, there's nowhere I see the VB Form or the webBrowser calling the HTML code or function closeIt() in order to be executed.


Friday, November 4, 2016 3:05 AM

Hi LetMeCode,

Sorry for the previous code has a little problem. And based on your html page, I tried to test by using WebBrowser control. I suggest you could set "window.onbeforeunload = function () { };" before the page is not laoding. Using the WebBrowser1_Navigated event to handle it. Please check the following:

Imports mshtml

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("https://www.gumtree.co.za/post.html")
    End Sub

    Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        Dim alertBlocker As String = "window.onbeforeunload = function () { };"
        WebBrowser1.Document.InvokeScript("execScript", New [Object]() {alertBlocker, "JavaScript"})
    End Sub
End Class

Hope it will be helpful to you.

Best Regards,

Neda Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Friday, November 4, 2016 11:00 AM

Thanks so much Mr Neda Zhang.

It's now working perfect!

Just wanna know one more thing.

The code your wrote in the HTML page and WebBrowser1_Navigated is it Javascript??

So, if I learn JavaScript will I be able to write/understand it??

If so, can you please suggest me a good JavaScript course.


Friday, November 4, 2016 11:28 AM

If so, can you please suggest me a good JavaScript course.

https://play.google.com/store/books/details?id=Wmfr1Zp7d5EC&source=productsearch&utm_source=HA_Desktop_US&utm_medium=SEM&utm_campaign=PLA&pcampaignid=MKTAD0930BO1&gclid=CJnHoo_-jtACFdbhDQodbdgCRA&gclsrc=ds

http://shop.oreilly.com/product/9781593272821.do

What you should do is stop using the Webbroswer crutch thing from a Windows form, and learn how to do real Web development.


Friday, November 4, 2016 1:09 PM

Thanks DA924x for the link.

What you should do is stop using the Webbroswer crutch thing from a Windows form, and learn how to do real Web development.

So basically give me a list of Programming languages that do real Web Dev.

cause I tried to use "WebRequest Class for performing POST or GET requests" here but other people in the Forum said I can't use it whether I want to deal with SPAN elements/Filling Web Form,... whereas Wikipedia says another thing here just in the first paragraph, last line. (It is often used when uploading a file or submitting a completed web form.)

That what is confusing me.

====

Okay, let get this clear.

Let assume, I want to post my offer here, Is it possible to do that with "WebRequest Class for performing POST or GET requests"???

Waiting for your replies?

**


Friday, November 4, 2016 1:37 PM

I can't tell you anything about the Webbroswer control. I have never used it and never ever plan to either.

Get yourself a book on AJAX that deals with Post and Get, get a book on JQuery too, and learn how to use ASP.NET.

 https://msdn.microsoft.com/en-us/library/aa286485.aspx?f=255&MSPPError=-2147217396

https://www.techopedia.com/2/29912/development/web-development/10-things-every-modern-web-developer-must-know

The part that it is talking about C#, you can replace the topic and use VB.NET in its place.


Monday, November 7, 2016 4:14 PM

Hi LetMeCode,

Sorry for the previous code has a little problem. And based on your html page, I tried to test by using WebBrowser control. I suggest you could set "window.onbeforeunload = function () { };" before the page is not laoding. Using the WebBrowser1_Navigated event to handle it. Please check the following:

Imports mshtml

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("https://www.gumtree.co.za/post.html")
    End Sub

    Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        Dim alertBlocker As String = "window.onbeforeunload = function () { };"
        WebBrowser1.Document.InvokeScript("execScript", New [Object]() {alertBlocker, "JavaScript"})
    End Sub
End Class

Hope it will be helpful to you.

Best Regards,

Neda Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Hi Neda Zhang,

your code was working perfect but now it's no longer working. I don't know why !!!

I realized that it was giving me errors saying that ( Runtime errors might occur when converting 'System.Windows.Forms.HtmlDocument' to 'mshtml.HTMLDocument' )

then I just added (System.Windows.Forms.) before every declaration. please see below code.

            Dim elements As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

            For Each pElem As HtmlElement In elements
                If pElem.GetAttribute("name") = "UserName" Then
                    pElem.SetAttribute("value", TextBox3.Text)
                End If
            Next

Monday, November 7, 2016 4:22 PM

Hi LetMeCode,

Sorry for the previous code has a little problem. And based on your html page, I tried to test by using WebBrowser control. I suggest you could set "window.onbeforeunload = function () { };" before the page is not laoding. Using the WebBrowser1_Navigated event to handle it. Please check the following:

Imports mshtml

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("https://www.gumtree.co.za/post.html")
    End Sub

    Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        Dim alertBlocker As String = "window.onbeforeunload = function () { };"
        WebBrowser1.Document.InvokeScript("execScript", New [Object]() {alertBlocker, "JavaScript"})
    End Sub
End Class

Hope it will be helpful to you.

Best Regards,

Neda Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Hi Neda Zhang,

your code was working perfect but now it's no longer working. I don't know why !!!

I realized that it was giving me errors saying that ( Runtime errors might occur when converting 'System.Windows.Forms.HtmlDocument' to 'mshtml.HTMLDocument' )

then I just added (System.Windows.Forms.) before every declaration. please see below code.

            Dim elements As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

            For Each pElem As HtmlElement In elements
                If pElem.GetAttribute("name") = "UserName" Then
                    pElem.SetAttribute("value", TextBox3.Text)
                End If
            Next

You mean you are unable to figure out why the code quit working after you altered the code? Why don't you alter it back to what it was before you altered it.

Then alter a single line that you altered to cause it to quit working till it quits working again. Then you will know what line that you altered that the code quit working at.

I realize there is effort required to do that but it can't be that much.

And it was giving you warnings and not errors which are two different things.

La vida loca


Monday, November 7, 2016 4:41 PM

You mean you are unable to figure out why the code quit working after you altered the code? Why don't you alter it back to what it was before you altered it.

Yes, you're right !! that's it.

I don't remember where I touched !!!

I can't alter it back cause I restarted the PC.

I checked; rechecked but I m will unable to bring it back !!

I don't know what's going on !!


Monday, November 7, 2016 4:46 PM

You mean you are unable to figure out why the code quit working after you altered the code? Why don't you alter it back to what it was before you altered it.

Yes, you're right !! that's it.

I don't remember where I touched !!!

I can't alter it back cause I restarted the PC.

I don't know what's going on !!

Well considering Neda Zhangs code is still in this thread I don't see how you can't alter it back. After all you said all you did was alter it to have System.Windows.Forms. propended to some lines. And then you said the code quit working.

In the future if you really don't know what caused the code to quit working then don't say what you did caused it to quit working.

So redo the code as Neda Zhangs code that you used originally if you want to fix it.

La vida loca


Monday, November 7, 2016 6:51 PM

You mean you are unable to figure out why the code quit working after you altered the code? Why don't you alter it back to what it was before you altered it.

Yes, you're right !! that's it.

I don't remember where I touched !!!

I can't alter it back cause I restarted the PC.

I checked; rechecked but I m will unable to bring it back !!

I don't know what's going on !!

If you installed and learned how to use TFS Express (free to download and install) or one of the other free code repository tools, you would never face the problem again, of changing or losing code and not be able to get a previous version of the code back.


Monday, November 7, 2016 7:14 PM

Thanks everyone, I think it's solved !!!

I just opened IE 11 and cleared all cache/data history and

launched the app again to test and realized it's now working great.

WebBrowser is not really efficient to do that job as it relies on IE that is a wreck like many other in this Forum keep saying and that's true.

Anyway, I'm gonna finish that version that uses webBrowser control and start a new one with HttpClient or Net.HttpWebRequest or AJAX or whatever that can do real Web Dev.

But I merely need a good advice cause they are many Technology/Software out there that do exactly the same think like for instance (HttpClient, Net.HttpWebRequest & AJAX) that all three is able to submit a Web Form with a file attached.

So which one should I use???  So confused !!!


Monday, November 7, 2016 10:56 PM

I just opened IE 11 and cleared all cache/data history and

Clearing cache where you have have  made changes to the HTML or to  JavaScript code where you don't have to restart the browser session is ctrl-ley and Refresh F5 or Refresh Icon on browser's address line. What you are talking about has no affect on any browser session currently running that you are doing some kind of development work and running the browser solution.

WebBrowser is not really efficient to do that job as it relies on IE that is a wreck like many other in this Forum keep saying and that's true.

No not really, the wreck is the Webbrowser control crutch and using it from a desktop solution. That's the tragedy in its own rights.

http://searchwindevelopment.techtarget.com/definition/Ajax

http://www.w3schools.com/xml/ajax_intro.asp**

You can toss AJAX in there and Jquery in there too.

 https://www.techopedia.com/2/29912/development/web-development/10-things-every-modern-web-developer-must-know


Thursday, November 10, 2016 10:03 AM

Hi LetMeCode

I am glad your issue has been resolved, I would suggest you mark the solution as answer, and then others could find the solution easily.

For new issues, I would suggest you post a new thread, and then we could focus on new issues.

Thank you for participating in the forum activities and sharing your issues.

Best Regards,

Neda Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.