make unsubscribe from javascript event in C# code Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of make unsubscribe from javascript event in C# code without wasting too much if your time.

The question is published on by Tutorial Guruji team.

i have got such a problem here:

i have a webpage in webbrowser control. that page uses javascript and had subscribed to number of events (i.e. click).

here is algorithm that needed:

  1. load page in webbrowser component
  2. in C# part of code force page to unsubscribe from javascript event

is there anyway to do that?

thanks in advance.

Answer

1. method: create the unsubscription functions in javascript and then call it from c# side,

in the page code:

<script type="text/javascript">
    // assign the event to the element
   document.getElementById("myElem").onclick = function(){
       //your code here
   }

   // clear the event
   function clearEvent(){
       document.getElementById("myElem").onclick = function(){
          return false;
       }
   }
</script>

in C# side:

IHTMLDocument2 doc = (IHTMLDocument2)IE.Document;
IHTMLWindow2 parentWindow = doc.parentWindow;
if (parentWindow != null)
    parentWindow.execScript("clearEvent();", "javascript");
}

2. method: call the unsubscription code directly from c#

IHTMLDocument2 doc = (IHTMLDocument2)IE.Document;
IHTMLWindow2 parentWindow = doc.parentWindow;
if (parentWindow != null)
    parentWindow.execScript("document.getElementById('myElem').onclick=function(){return false;}", "javascript");
}
We are here to answer your question about make unsubscribe from javascript event in C# code - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji