Jump to content

preventing a page unload


skaterdav85

Recommended Posts

Is there any workaround to stopping an unload event? I have Googled it several times and it seems you cannot stop the unload event, but maybe someone has found a workaround? I am using jQuery though so if anyone knows a workaround or a jQuery method that can help me, that'd be great!

Link to comment
Share on other sites

Would that not be a security risk?Think about it. You inadvertently end up on a malicious website and try to leave. But the script on the page prevents the unload event, forcing you to stay on that page, downloading numerous trojans and spywares and broadcasting who knows what.

Link to comment
Share on other sites

True, if my site gets attacked and has spyware and/or trojans. However, this site is part of an internal company application so I don't think that will happen.The motivation for this was to alert the user that they must click the Save button prior to leaving the page (like clicking a link or navigating elsewhere with the browser back button or typing in a new url) if they want any of their information typed into the form fields to be saved.

Link to comment
Share on other sites

The point is that the browser doesn't know if you are a good guy or a bad guy.Put yourself in the user's position. Wouldn't it be annoying as heck if you tried to close a window and could not? Tried to go forward or back in your history and could not?If you have permission to use the technique carefully, then everyone else has permission to use it maliciously.

Link to comment
Share on other sites

Well I want to give the user a choice by showing a confirmation box. If the user clicks OK, then the event wont stop and the user will leave the page. If the user clicks Cancel, then the event will be stopped, allowing them to press Save prior to leaving the page. I agree that it would be bad to stop the user from leaving without any say, but it seems useful to tell them to press Save before leaving a page if form fields have been modified, wouldn't you agree?I finally found some code that worked and modified it a bit. Apparently if you assign a function that returns a string to the onbeforeunload event of the window, it will display a confirmation box for you, and your choice will either continue to leave the page or stop you from leaving the page. I never even heard of 'onbeforeunload' until I was given this task.

(function (J) {		var setConfirmUnload = function () {			window.onbeforeunload = function () {				var message = 'You have entered new data on this page.  If you navigate away from this page without pressing Submit, the changes will be lost.';				return message;			}		}		// Prevent accidental navigation away		J('textarea, select').bind("change", function () {			setConfirmUnload();		});})(jQuery);

Link to comment
Share on other sites

Well I want to give the user a choice by showing a confirmation box. If the user clicks OK, then the event wont stop and the user will leave the page. If the user clicks Cancel, then the event will be stopped, allowing them to press Save prior to leaving the page. I agree that it would be bad to stop the user from leaving without any say, but it seems useful to tell them to press Save before leaving a page if form fields have been modified, wouldn't you agree?
I certainly agree. And I've found myself wanting to do this same thing several times. But the fact remains, that there are just too many malicious people in this world to allow browser manufacturers to implement this sort of thing. That's just a fact of life.
Link to comment
Share on other sites

onbeforeunload in one of those Microsoft proprietary things that does not belong to the standard, but which other browsers have adopted. Firefox deprecates its use.I suppose I could have told you that right off . . .

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...