Asked by:
Detach the onbeforeunload event handler of the body tag

Question
-
Hi,
I need to detach the OnBeforeUnload event handler of the <BODY> tag in CRM 2013. While there is a method to accomplish that in CRM 4.0 (http://social.microsoft.com/Forums/en-US/38c1186b-55fd-4cf5-bcea-5e68cf2d2889/how-to-detach-event-onbeforeunload-or-onunload?forum=crm), I can't find out how to do this in CRM 2013.
Any suggestions?
Thanks,
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.comMonday, August 11, 2014 2:00 PM
All replies
-
The following link on my blog presents a simple implementation of xfrm.Close.
The Close method accepts a "suppress all fields flag" which is similar to detachCloseAlert method.
The method changes each field submit mode to never. This prevents the dialog from showing on dirty forms.You can implement the same for your own detachCloseAlert function.
Cheers
Dev Blog: Dynamics CRM - Thinking outside the Box
Monday, August 11, 2014 4:24 PM -
Thanks Adi but I'd like to just attach this piece of JavaScript code to the BODY tag of the current page:
onbeforeunload='document.write("closing window...");'
so it would look like this:
<body onbeforeunload='document.write("closing window...");'>
I don't want to build a framework to accomplish that. Is it possible to do that with 1-3 lines of code in CRM 2013?
Thanks,
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.comMonday, August 11, 2014 5:20 PM -
The equivalent of detachCloseAlert in 2013 is
Mscrm.RefreshPageHandler.set_forceNavigationAway(true);
If you just want to change the text of the onbeforeunload message you can change this variable
LOCID_FORMS_SAVE_CONFIRM_TITLE = "Leaving so soon ???";
This is heavily unsupported and will blow up in our faces soon enough … so enjoy it while you can J
Cheers
Dev Blog: Dynamics CRM - Thinking outside the Box
Monday, August 11, 2014 10:21 PM -
Thanks Adi!
What I'm trying to accomplish is to get rid of the annoying message that pops up when I redirect away from a page:
I have figured out that it is a matter of removing the OnBeforeUnload event handler of the BODY element on the page. Unfortunately, the JavaScript code Mscrm.RefreshPageHandler.set_forceNavigationAway(true);
does not seem to remove this handler. I still get the same message box when I leave the page.
Any other suggestions?
Thanks,
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.comWednesday, August 13, 2014 6:35 PM -
That’s what happens when you start messing with CRM internals.
The first line above worked in chrome.
The following line seems to work in IE and chrome. Haven’t tested other browsers / all form types.
Mscrm.ReadFormUtilities.set_forceNavigationAway(true);
Cheers
Dev Blog: Dynamics CRM - Thinking outside the Box
Wednesday, August 13, 2014 8:34 PM -
Thanks Adi for your help. I have tried both code snippets in the Form.OnLoad as well as IFrame.OnReadyStateChanged but the Message Box still appears when I leave the page.
I used IE 11 and Chrome.
Thanks,
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.com- Edited by ata6502 Thursday, August 14, 2014 1:43 PM
Thursday, August 14, 2014 1:36 PM -
I’ve tested with IE 8,9 and chrome. Did not encounter any issues with these browsers.
The best that I can offer at this stage is to put the debugger; keyword before the code and test where and why it fails.
Which type of entity / form are you trying to force away (detach) from ? is it Custom or System/Customizable entity ?
Cheers
Dev Blog: Dynamics CRM - Thinking outside the Box
Thursday, August 14, 2014 9:17 PM -
Thanks Adi.
I came up with this convoluted piece of code that erases the body's onbeforeunload event handler:
var body = window.frames[0].parent.document.getElementsByTagName('body')[0]; body.onbeforeunload = null;
I put it into OnLoad as well as in OnReadyStateComplete of the problematic IFRAME. I added the alert() statements that show that the event handler is erased indeed. Regardless of that the "Are you sure you want to leave this page?" dialog box is still displayed.
The IFRAME is embedded in a custom entity form. It links to a Navision Card form which is a part of Navision Web Client.
I'd really like to know if it is even possible to accomplish what I'm asking about: getting rid of the annoying "Are you sure you want to leave this page?" dialog box when the user leaves the page.
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.comMonday, August 25, 2014 3:48 PM