Discussion Locked

IE6: Stopping Navigation away from page

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am currently working on an application where I need to query the user before they navigate away from a page. For example, when users have changed data on the screen, but have not yet saved it and they try to move to another page they are shown a warning message.

To achieve this effect I am using the onbeforeunload event of the window object. I have a page with the following code:

Test Page

This is a test of the event.returnValue code. click here

In IE6, when you click the button, the message box is displayed, but if you click "Cancel", a javascript "Unspecified Error" occurs. Strangely, this error onlyoccurs when the navigation is done by changing the 'window.location.href' property. If you click the link on the page, the message box and cancel functions behave correctly.

This error only occurs in IE6 and not in IE5.5. Has anyone encountered this problem before?

4 replies

by jay eckles

Well, the first thing to say about your code is that onBeforeUnload is a Microsoft-specific event. I would definitely go with onUnload, but that probably doesn't do what you want. onBeforeUnload probably won't work in non-IE browsers.

The way Iimplemented the functionality I think you're going for is to add an onChange event to every form input element on teh page which set a page level variable when the element was changed. Then I added an onClick event handler to each and every link onthe page to check that variable, and if it indicated a change, I would pop up a message saying, "Are you sure" or somethign to that effect.

This method does not prompt the user if he or she leaves via browser controls (like back), but it does work in any Javascript or ECMAScript compliant browser.

I have to say that I also find the assignment of a string to event.returnValue a bit strange - I haven't seen it before and I'm not sure what I would expect to happen. The only assignment to event.returnValue that I've ever seen before has been a boolean value - true for allow the event to continue and false for stop the event. I'll have to do a little research on this.

You may be having the troubles only in IE6 because it is more standards compliant. I don't know though.

Good luck.

Jay
www.jayeckles.com

by barbs

The application I am working on is for our corporate intraweb. Luckily we have strict standards on the intraweb and the only browser available to users is Internet Explorer, so I am able to take advantage of IE specific features. The advantage of using the onBeforeUnload event is that it is triggered by any of the actions that can cause a user to move away from the page.

The assignment of a string to the event.returnValue property is a trick that I picked up someone elses code somewhere. Itcauses IE to display a dialog box asking the user if they want to navigate away from the page. The box has an OK button and a Cancel button to allow the user to proceed away from the page or to remain on the page if they choose.

If you have any other suggestions or comments, I would be most grateful.

by barbs

I have implemented your suggestion and it works really well.

Thanks for you help!!

This discussion is closed to new replies.