English 中文(简体)
C# WebBrowser
原标题:Disable Javascript click events in C# WebBrowser

Intro:
I m working on a C# WinForms + WebBrowser project.
I d like to do some processing upon a click on a hyperlink.

在这里,我是如何在超文本链接上用超文本文件对每一个超级链接进行点击的:

void webBrowser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e )
{
    HtmlElementCollection links = webBrowser.Document.Links;
    foreach ( HtmlElement link in links )
    {
      link.Click += delegate { MessageBox.Show( "C# Click!" ); };
    }
}

The Problem:
The above code works well, but the problem is that I get side effects from javascript code which also performs some clicking action.

I learned the hard way that this problem comes in many shapes and forms..
Here s a not so obvious example of a jQuery action that makes life harder:

$(document).ready(function() {
  $("a").click(function() {
    alert("jQuery Click!");
  });
});
<html>

<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>>
</head>

<body>
  <a href="#">Click Me!</a>
</body>

</html>
<html>

<body>

这里发生的情况是,在<代码>WebBrowser控制”中点击超链接时,“jery Click”首先会消失,而且只有在“C# Click”电文被丢弃之后才能显示。

The Question:
I ve searched far and wide, but didn t yet come up with an answer.

我怎么能用“C#-programmatically”来防止在网上浏览器上实施j Query或任何其他形式的javascript代码?

Clarification:
I d like to say I already tried to alter the document in many ways, e.g. stripping <script> tags from webBrowser.DocumentText, and from the inner html node, and by working with HtmlElement and the underlying IHtmlElement objects...

但无uck。

更改<代码>webBrowser.DocumentText 造成文件重载体,因此,我失去我到原址的任何全权证书(例如厨师)。 改写的<代码>IHtmlElement,物体确实起作用,致使 j本已经装上,我再次无法阻止事件的发生。

问题回答

To my knowledge there is no way to disable JavaScript execution in WebBrowser component. The only you can do is to suppress the error messages with ScriptErrorsSupressed property set to false. An idea would be to pre-filter your HTML and eliminate all your "onclick" events from HTML using regular expressions before you render your HTML in the WebBrowser component.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签