windows.onbeforeunload not consistently firing...

davism
davism used Ask the Experts™
on
Hi all,

I have a question that maybe I'm missing something.

I have a masterpage and in it I have a window.onbeforeunload={some confirmation function}.

Then in the content page I have a window.onbeforeunload={some other confirmation function};

Now, the problem I am having is consistency; in some cases I can repeatedly get the confirmation information and in some cases (only data differences) I do not get the prompt.

I even changed it to something like this: window.onbeforeunload = alert('firing');confirmExit;

On the situations cases where I do not get the prompt I obviously do not get the alert and otherwise I do.

The fact of not getting the alert indicates that it is not firing. What would cause something like that?

Just an FYI--> I am using something very similar to this: http://aspnet.4guysfromrolla.com/articles/101304-1.aspx

Any information would be greatly appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Have you tried window.onbeforeunload = "Your alert message";
I seem to remember that it works that way.

Ben
leakim971Multitechnician
Top Expert 2014

Commented:
you may have a bug and at this moment javascript stop to run.

usefull page about onbeforeunload : http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

Author

Commented:
leakim971,

I think you're ok track with that because I did note I have a javascript error. But that javascript error is on that one that I can repeatedly reproduce that the change awareness box is not present.

The web page rendering is the same. I'm not sure what would trigger the error through.

The error is coming from this snippet of code that is injected:

"<script language=""JavaScript"">assignInitialValuesForMonitorChanges();</script>")

On the records where it is not doing the change awareness I am getting an error on that line that indicates an invalid object name.

Any thoughts on that?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

leakim971Multitechnician
Top Expert 2014

Commented:
we need to open box of assignInitialValuesForMonitorChanges
else to be sure a "stupid" (fast?) resolution :

(by this way you will be able to see what miss in your page or its processing)
<script language=""JavaScript"">
try  {
// we don't worry about error(s) caused by 
   assignInitialValuesForMonitorChanges();
}
catch(e) {
}
</script>

Open in new window

Author

Commented:
Hmmm...I'm not sure I'm following on that because if it abends it will hit the catch block and do nothing. But the assignInitialValuesforMontiorChanges() is essential in the effectiveness of capturing the changes.

I will try it through - but I won't be able to until tomorrow A.M.
leakim971Multitechnician
Top Expert 2014

Commented:
So if possible provide code of assignInitialValuesforMontiorChanges() or better a live link

Author

Commented:
leakim971Multitechnician
Top Expert 2014

Commented:
I need to reproduce the bug
Michel PlungjanIT Expert
Top Expert 2009

Commented:
@Ben: It is
window.onbeforeunload=function() {
  if (somereasontoalert) return "You will lose your data"
}

Commented:
@mplungjan : you're right, my mistake.

Author

Commented:
Ok, found the culprit. I'm not too sure how to get around this though. I'm not too sure if I should provide the points for this question and then open another. They are in a way related so I'm not too sure.

Here is the situation:

I did a costimation to the linked code providing in the OP (Original Post). The customization is that I took the original state values which were in a session variable and assigned them to a java script variable and then did the code injection:

strScript.AppendLine("   var arrOrig='" + Session["origState"] + "';");
strScript.AppendLine("   monitorChangesValues=arrOrig.split('~');");

Now, one of the fields in the arrOrig a list of text. Everything is working fine EXCEPT when a there is a carriage return life feed in the text. When that happens the arrOrig is choking in a sense because the closing "';" is not being recognized in the var statement. So, that is throwing an issue which is further causing the other error that this question is about. (this is how it's related.)

Now, my question is HOW would/could I work with a field having a carriage return line feed (basically a \r\n) to put able successfully put into that arrOrig array?

Hopefully, this makes some sense.
Multitechnician
Top Expert 2014
Commented:
Are you able to remove the cr\lf ?
strScript.AppendLine("var arrOrig='" + Session["origState"].replace(/\\n|\\r/g,"") + "';");

Open in new window

Author

Commented:
Cool. The only difference is that I got about it by when I loaded the array I put a .Text.Replace("\r\n","@RETURN") so I knew where was at. I suppose I could've done the same with:

 strScript.AppendLine("var arrOrig='" + Session["origState"].replace(/\\n\\r/g,"@RETURN") + "';");

Works both ways. Either way, it worked out.

Thanks!

Author

Commented:
Actually, other way around:

.replace(/\\r\\n/g,"@RETURN"

Carriage control line feed not line feed carriage control. :-)

leakim971Multitechnician
Top Expert 2014

Commented:
You're welcome! Thanks for the points! Have a nice week-end!
leakim971Multitechnician
Top Expert 2014

Commented:
/\\n|\\r/g should replace \n OR \r (mean any order, and the g added to the regex for any occurence as you know)

more info here : http://en.wikipedia.org/wiki/Regular_expression
(look for The choice)

Author

Commented:
Yeah...good point. I'm just used to seeming them together.

Cool call though and thanks!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial