开发者

HtmlWindow.Error event won't fire on Javascript error

I'm working on a WinForms control that wraps a Javascript API using a WebBrowser control, so I need to trap Javascript errors and convert them to exceptions.

_browser.Navigated += BrowserNavigated;
_browser.DocumentText = "...";

private void BrowserNavigated(object sender, WebBrowserNavigatedEventArgs e) {
    _browser.Document.Window.Error += ScriptError;
}

private void ScriptError(object sender, HtmlElementErrorEventArgs e) {
    MessageBox.Show(e.Description);
    e.Handled = true;
}

I know this method doesn't work if the page is refreshed, but that will never happen in my case, so I'm trying to keep things simple. The Browser开发者_开发技巧Navigated method is executed after setting DocumentText, but when an error occurs, the ScriptError method doesn't get called.

Any ideas?

UPDATE: Attaching the Error handler in the DocumentCompleted event instead of Navigated now fires the script error handler for some errors. However, if a Javascript file specified by a <script> tag fails to load and then I try to use a function from it, I still get the generic error dialog.

This is main case in which I need to catch the error, because if the Javascript file doesn't load, it means the user's API key is invalid. How is this error different from the others?


Try attaching the handler for ScriptError outside the BrowserNavigated Event like you did for _browser.Navigated += BrowserNavigated;.. What currently happening is when BrowserNavigated fires then only the handler gets attached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜