problem with beforeunload event in WebBrowser control c# winform

218 Views Asked by At

I have written logic to logout from web application in beforeunload even, as follows:

window.addEventListener("beforeunload", function (event) {  
   //Logout logic
   //Calling logout api with values from session
   //clearing the session on success 
});

it is working fine in all web browsers (IE, Mozilla, Chrome), however It is not working from WebBrowser control in winform application.

What could be the reason behind it?

PS: there is nothing wrong with javascript, when I call this same script by invoking it as below, it works fine:

   HtmlElement head = webBrowser4.Document.GetElementsByTagName("head")[0];
   HtmlElement scriptEl = webBrowser4.Document.CreateElement("script");
   IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
   element.text = @"function Logout() {
   $.ajax({
         url: '/api/Logout',
         type: ""GET"",
         contentType: ""application/json;charset=utf-8"",
         success: function(returnVal) {
                    },
         complete: function() {
                     sessionStorage.clear();
                    },
         error: function(returnVal) {
                        }
                    });
                 }";
     head.AppendChild(scriptEl);
     webBrowser4.Document.InvokeScript("Logout");

What is the problem with beforeunload event in WebBrowser control in winform?

1

There are 1 best solutions below

0
On

Did you try it without listener?

window.onbeforeunload = function(event)