2

I've got a problem to handle the window.onbeforeunload event in a JS-Application in a HTML-web resource for Microsoft Dynamics CRM 2011. By using the "normal" IE the following code works fine:

window.onbeforeunload = function (e) {
    if (changedData) {
        var message = 'leave...';
        if (typeof e == 'undefined') {
            e = document.parentWindow.event;
        }
        if (e) {
            e.returnValue = message;
        }
        return message;
    }
}

But in the CRM 2011 Outlook client, I don't get the leave message. Have you any idea to get a leave message when closing the window?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

The CRM "object stack" in Outlook versus IE is likely to be different (since if nothing else, Outlook container windows are involved to wrap the IE Iframes) so when you are using "undocumented" events (in the CRM SDK sense) then you will be susceptible to such "surprises" ;)

What do you expect to happen with your code? You are only setting a returnValue on an event - this alone will not result in any user feedback.

Have you confirmed if your code is running at all (i.e. inserting an alert();)?

Greg Owens
  • 3,878
  • 1
  • 18
  • 42
  • Is the onbeforeunload event unsupported? I didn't find it in the SDK. Is there an equivalent for the onbeforeunload event? The code shows, if the event is fires, a window with the message. Like if do any changes in a CRM entity form and close the window without saving. --> like this: [link](http://stackoverflow.com/questions/6218468/javascript-onbeforeunload-function) The code works outside of the Outlook Client. I've test it with alert() and in in the IE web browser it works fine, all alerts will be shown, but in Outlook client the code isn't call. – user1829815 Nov 20 '12 at 10:02
  • Sorry I should clarify - when I say "unsupported" I mean that when scripting within the context of CRM, Microsoft only guarantees that some things work and will continue to work. The fact that `onbeforeunload` does not exist in the CRM SDK at all means that it is not "supported" for CRM. Thanks for clarifying what your code should be doing via the link (I learned something from that). Although I suspect that you are falling victim to [this](http://stackoverflow.com/questions/1728495/window-onbeforeunload-not-firing-in-child-window?rq=1)... – Greg Owens Nov 20 '12 at 10:12
  • ..., perhaps you could try putting a `debugger;` statement into line 1 of your function and inspect what is happening at runtime? One other thought - is this a custom entity or are you using the CRM forms in Outlook? (I know this is a basic question, but just checking).If you are using, for example, the Outlook form for email or task then this code won't be loaded there. – Greg Owens Nov 20 '12 at 10:13
  • While loading the page the function is put into the event, but the event isn't call if I close the window. I'm also trying to set the event in the parent window, but it isn't calling too. The script can be opened on two different ways: In an Iframe at the account entity form and as a "stand alone" web resource in an own window. – user1829815 Nov 20 '12 at 12:30
  • I'd need to test this on my machine as there could be a few things going on here (I'm thinking either the issue I linked to, 2 comments ago or perhaps the onbeforeunload event handler being overwritten or something else...). For the sake of testing, is the behaviour the same using the `onunload` event? – Greg Owens Nov 20 '12 at 14:32
  • The behavior between onUnload and onBeforeUnload are different I think. The onUnload is fired while the window is closing, the onBeforeUnload before. So you can’t interrupt the onUnload event by a user feedback like the leave message. By the way, I’ve tested the onUnload yet and it’s only fired in the iframe on the account form. – user1829815 Nov 20 '12 at 15:01
  • Another note, i've tested a litte bit and find out, that the onBeforeUnload works and show the message by reloading and navigate out of the web ressource. I don't know why it isn't fired by closing the window... – user1829815 Nov 21 '12 at 09:30
  • Hi Greg, have you find anything out to this topic? – user1829815 Dec 03 '12 at 10:56
  • Not had time to run any tests I'm afraid - busy with the day job ;) I still think that this page explain why it is probably not working though: http://stackoverflow.com/questions/1728495/window-onbeforeunload-not-firing-in-child-window?rq=1 – Greg Owens Dec 07 '12 at 09:55
  • No problem :-) - I've test it directly in the body of the HTML page too - and it didn't work on close... – user1829815 Dec 07 '12 at 12:43