1

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";
    }
}
Community
  • 1
  • 1
  • A bit OT, but IE6, really? It's over 10 years old and even Microsoft doesn't want to support it anymore, see http://www.ie6countdown.com/. Just a guess, but would you receive the same error with another version of the browser? – Abel Jan 17 '12 at 14:57
  • I knew I would get a comment about IE6. Unfortunately yes IE6, this is the world I'm currently living in and have to continue living in until 2nd maybe 3rd quarter of this year. – Edward Heyne Jan 17 '12 at 15:26

1 Answers1

0

The error translates to 0x80020101, which in turn translates to SCRIPT_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:

  • try the suggested alternative approach in the referred thread (if you haven't tried so already)
  • create a handler for 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.

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247
  • I did already have the alternative approach. I believe that the issue may be in the javascript. However, when I change the javascript to where I no longer get an error, the `result` param of the Invoke always comes back as null, thus a message box with no text appears. – Edward Heyne Jan 17 '12 at 17:14
  • Ok, I was able to find some javascript that worked. Thanks for you help Abel. – Edward Heyne Jan 17 '12 at 17:24
  • @Edward that probably means that the JS you used previously contained an error. For instance, that in the above example cObject didn't exist or something. – Abel Jan 17 '12 at 20:46
  • The error is actually caused by the 'event' object not being known, so is null. Is there a way to make 'event' known in the solution linked in above. – Edward Heyne Jan 20 '12 at 19:29
  • @EdwardHeyne: so it is indeed a JavaScript error. You should use `window.event` instead, works only on IE. If your script should run on other browsers as well, use this technique: http://www.quirksmode.org/js/events_access.html – Abel Jan 21 '12 at 11:36
  • So I tried the `window.event` and now I get a javascript error saying **window.event is null**. I'll look into the link provided, but for now I will only be using IE. – Edward Heyne Jan 23 '12 at 18:01
  • @EdwardHeyne: I don't know why the event object appears null. Try to run the script in a browser to test it. Or use a global variable to hold the return value (ugly, I know, but this seems either an IE bug or a JS bug and normally, `window.event` is always available...). Also, just for testing, try other scripts in the onbeforeunload handler, to be sure it runs at least some trivial JS. – Abel Jan 24 '12 at 11:18
  • The JavaScript is not the issue. It works perfectly fine in IE. The issue is in the method used to force the onbeforeunload to fire. It does not appear to have an event object and I would like to know if there is a way to create the event object and pass it into the Invoke method call. – Edward Heyne Jan 24 '12 at 15:33
  • @EdwardHeyne: this question is called "how to _handle_ the onbeforeunload event", now it seems that you want to mimic it instead. That's never gonna work 100%. What you are doing is just calling the event handler, which is equal to calling a function. You can create the event by hand (`window.event = {};`). This prevents `null`, but is not the event object, because the event wasn't fired. A general approach with "calling the code inside an event" is just writing a function. You call that function from inside the event, and you can call it from any other place. – Abel Jan 24 '12 at 23:32