Monday, May 09, 2005

Getting Rid of the Leave Warning Alert after a Preview

Have you tried turning off the Leave Warning alert only to find that despite repeated attempts to get rid of it, it seems to reset itself after a Preview? And do you find that this problem occurs more frequently for templates that make use of complex controls, like DataGrids and Calendars that require a post back to the server?

You've already set the EnableLeaveAuthoringWarning attribute to false on the Authoring Console. Or alternatively, you have implemented Stefan's sample to turn off the leave warning for a particular control. Somehow, the warning goes away for a while, but after a Preview, the EnableLeaveAuthoringWarning flag seems to reset itself and appears once again.

Try it:
1. Add an ASP.NET Button to a template file.
2. Set the EnableLeaveAuthoringWarning flag of the Authoring Console to false.
3. Click Preview.
4. Close the Preview window.
5. Click on the Button you have added to the template. The alert appears despite being disabled!

Last week, we finally found the reason. The reason lies in the CMS_restoreAfterPreviewPostback() Javascript routine that is called after the Preview button is clicked.

In that routine (found in the Console.js file), the g_bWarnBeforeLeave global variable, which decides whether or not the leave warning appears is reset to true:

function CMS_restoreAfterPreviewPostback()
// restore state of form

. . . code continues . . .
g_bWarnBeforeLeave = true;

We need to set it to false when EnableLeaveAuthoringWarning is false. I'm not sure if modifying the Console.js file violates any support agreement with Microsoft. Anyway, it's probably not a good idea to do so as the next service pack will just overwrite all changes that we have done to it.

So to get around this, we will take advantage of the fact that the routine is written in a linked script file. This means that if we were to embed a routine of the same name within postings, our custom version will run instead of the linked routine.

We added the following code to the DefaultConsole.ascx.cs file. The script basically re-generates the Cms_restoreAfterPreviewPostback() method, but instead of always setting the g_bWarnBeforeLeave flag to true, it is assigned the value of the Console's EnableLeaveAuthoringWarning property. So if the leave warning is turned off, it stays off!

private void Page_Load(object sender, System.EventArgs e)

private void GeneratePreviewScript()
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language=\"javascript\">\n");
sb.Append("function CMS_restoreAfterPreviewPostback()\n");
sb.Append("// restore state of form\n");
sb.Append("__CMS_PostbackForm.action = g_strOriginalFormAction;\n");
sb.Append(" = g_strOriginalFormTarget;\n");
sb.Append("g_bWarnBeforeLeave = " +

Console1.EnableLeaveAuthoringWarning.ToString().ToLower() +


I'm not sure why the preview button resets the flag in the first place, but at least with this workaround, our users will continue to receive a pleasant authoring experience!


At 9:45 PM, Anonymous Stefan Goßner [MSFT] said...

Nice tidbit! But my solution about disabling for a specific control should not be affected by this problem. Did you double check?

At 9:59 PM, Blogger Mei Ying said...

I did. Your solution does a nice job. But after the Preview button is clicked, the alert pops up again!

At 3:28 AM, Blogger Chester said...

Good work.. MCMS 2002 SP1a Console.js has this bug, if I'm correct (it was not there in MCMS 2002!).. I'm not sure changing Console.js will break the support boundary. If not that is an easy way to achive this.

I also had this issue before and came up with a somewhat different solution - a modified version of default console - modified version of PreviewAction ->

onclick="if(g_bWarnBeforeLeave){<*# Container.ActionJavascript*>}else{<*# Container.ActionJavascript *>;WBC_offWarningBeforeLeave()};return false"

Replace * with %

At 3:35 PM, Blogger Mei Ying said...

Interesting solution, Chester! That's definitely an alternative way to go about it.

At 4:55 AM, Blogger Chester said...

EnableLeaveAuthoringWarning - A Closer look


Post a Comment

<< Home