make unsubscribe from javascript event in C# code

822 views Asked by At

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.

1

There are 1 answers

7
Taha Paksu On BEST 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");
}