简体   繁体   English

取消订阅C#代码中的javascript事件

[英]make unsubscribe from javascript event in C# code

i have got such a problem here: 我在这里有这样的问题:

i have a webpage in webbrowser control. 我在webbrowser控件中有一个网页。 that page uses javascript and had subscribed to number of events (ie click). 该页面使用javascript并已订阅了许多事件(即点击)。

here is algorithm that needed: 这是所需的算法:

  1. load page in webbrowser component 在webbrowser组件中加载页面
  2. in C# part of code force page to unsubscribe from javascript event C#中的代码强制页面取消订阅javascript事件

is there anyway to do that? 反正有这样做吗?

thanks in advance. 提前致谢。

1. method: create the unsubscription functions in javascript and then call it from c# side, 1.方法:在javascript中创建退订函数,然后从c#端调用它,

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: 在C#端:

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# 2.方法:直接从C#调用取消订阅代码

IHTMLDocument2 doc = (IHTMLDocument2)IE.Document;
IHTMLWindow2 parentWindow = doc.parentWindow;
if (parentWindow != null)
    parentWindow.execScript("document.getElementById('myElem').onclick=function(){return false;}", "javascript");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM