Link to home
Start Free TrialLog in
Avatar of Marc Davis
Marc DavisFlag for United States of America

asked on

windows.onbeforeunload not consistently firing...

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.
Avatar of BenMorel
BenMorel

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

Ben
Avatar of leakim971
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
Avatar of Marc Davis

ASKER

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?
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

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.
So if possible provide code of assignInitialValuesforMontiorChanges() or better a live link
I need to reproduce the bug
@Ben: It is
window.onbeforeunload=function() {
  if (somereasontoalert) return "You will lose your data"
}
@mplungjan : you're right, my mistake.
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.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Actually, other way around:

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

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

You're welcome! Thanks for the points! Have a nice week-end!
/\\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)
Yeah...good point. I'm just used to seeming them together.

Cool call though and thanks!!