如何使用 winform webbrowser 处理 onbeforeunload 事件
我在 WinForms WebBrowser 控件和 onbeforeunload javascript 事件处理方面遇到了与本文完全相同的问题(如何拦截 WebBrowser 控件中的 onbeforeunload 事件?)。我尝试了与所提供的相同的解决方案,将 onbeforeunload 事件转换为 IDispatch 并调用 Invoke 方法。这样做时我收到此错误:
小时=-2147352319
我正在 WinXP 上使用 IE 6.0 在 .NET 3.5 中进行开发。
我没有将 C# 代码放在这里,因为它与上面链接的文章完全相同。
JavaScript 代码是:
function onBeforeUnload()
{
if (window.cObject.isTransferred)
{
event.returnValue = "\nYou are requesting to exit.\n";
}
else
{
event.returnValue = "\nYou are DELETING this object!!!\n";
}
}
I am having the exact same issue with a WinForms WebBrowser control and the onbeforeunload javascript event handling as this post (How to intercept the onbeforeunload event in a WebBrowser control?). I tried the same solution as presented to cast the onbeforeunload event to IDispatch and call the Invoke method. I get this error when doing so:
hr = -2147352319
I'm developing in .NET 3.5 on WinXP using IE 6.0.
I did not put the C# code here because it is exactly the same as the linked article above.
The JavaScript code is :
function onBeforeUnload()
{
if (window.cObject.isTransferred)
{
event.returnValue = "\nYou are requesting to exit.\n";
}
else
{
event.returnValue = "\nYou are DELETING this object!!!\n";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误会转换为
0x80020101
,进而转换为SCRIPT_E_REPORTED
(这是您自己执行此操作的方法)。 Microsoft 有一个已确认的错误,他们称该错误经常发生在多核或多处理器计算机上,但也可能也发生在单处理器计算机上。 Microsoft 解释说这与 COM 接口的调用有关。这支持了您提到的线程,用户最终遵循 Hans Passant 的答案,但收到此错误。通过更改 P/Invoke 声明,他设法解决了问题(答案写在问题本身中)。
然而另一个消息来源表明这也可能意味着脚本本身存在错误。如果是这种情况,您应该能够通过处理
onScriptError
来捕获脚本错误。我的建议:
onScriptError
创建一个处理程序编辑:OP 尝试了一些建议,发现这是最重要的JavaScript 错误(我的第二个建议):应该使用
window.event
,而不是event
,或者使用此 Quirksmode 文章。The error translates to
0x80020101
, which in turn translates toSCRIPT_E_REPORTED
(here's a how-to do this yourself). Microsoft has a confirmed bug that they say occurs often on multicore or multi-processor computers, but may occur on single processor computers as well. Microsoft explains that it has to do with the invocation of the COM interfaces.This backs up the story in your referred to thread, where the user eventually follows Hans Passant's answer, but receives this error. By changing the P/Invoke declarations, he manages to solve the problem (the answer is written in the question itself).
Yet another source suggests that this might also mean an error in the script itself. If that's the case, you should be able to catch the script error by handling
onScriptError
.My suggestions:
onScriptError
EDIT: the OP has tried some suggestions and found out that this was above all a JavaScript error (my second suggestion): instead of
event
,window.event
should've been used, or a common event access technique as described in this Quirksmode article.