Click to See Complete Forum and Search --> : overriding onbeforeunload and preventing user from navigating away by showing alert


asifone
April 2nd, 2009, 12:22 PM
Hi,

I have been stuck with this requirement for past 1 week , lets get started .

When user comes on to the page , i have to force user to complete the details wihtout navigating or clickin gon any other menu.

My requirement is as user tries to click on any other menu without saving his details...we should throw him with alert('please save before navigating anywhere else') and after clicking OK button,... force him to stay back on the same page and do not allow him to navigate to other page.

<script type='text/javascript'>
function showalert()
{
alert('please save before navigating anywhere else');
return false;
}

if(window.body)
window.body.onbeforeunload = showalert;
else
window.onbeforeunload = showalert;
</script>

PROBLEM:---

As i return false after alert ...the onbeforeunload displays it confirm box with "false" asa string or meaage in confirm box....here user can click OK buttton and navigate away.I ave to force user to stay on same page , fill details ,,click SAVE and then you can navigate.

I have to write the function in onbeforeonload event only as i cannot capture it anywhere else..and this event gives me confirm box...i only want on this event to show myalert
and stay then an there .

I cannot use history.go(-1) for security reasons..i have tried window.locatoin.reload() ..but here it shows my alert twice even before reloading.?

Please help me

Any help would be greatly appreciated.

Thanks!!!!!!

PeejAvery
April 2nd, 2009, 05:24 PM
JavaScript's onbeforeunload event cannot block a page closing completely unless the user invokes some confirmation. Using just a plain alert should not work because their isn't really a choice presented to the user. Try the following instead...

function showalert() {
if (!confirm('Are you sure you want to exit without saving changes?')) {return false;}
else {return true;}
}

asifone
April 5th, 2009, 02:03 AM
Thanks for reply..but

i do not want to give user a choice ..i do not want user to navigate away from my page untill he finishes his details on the current page....

If i use confirm box.....and if the user clicks on OK button after reading the message , he would get navigated to other page.....

if you can see ..the window.event.onbeforeunload = showalert()..now if i return true/false from the showalert function ..the value of window.event.onbeforeunload will be true/false

now the problem is: -
the window.event.onbeforeunload will throw its own confirm box again....with true or false as the message displayed in that confirm box.here again user will get a chance / option to navigate away .

I have to force user to complete the details and stay on this page untill he finishes and saves it.

Thanks

PeejAvery
April 5th, 2009, 08:40 AM
As I mentioned in my previous post. There MUST be an option for the user to leave...for browser security purposes. This is a good thing!

Think about this scenario. An infected webpage is loaded into your browser and is installing malicious content to your computer. Now, the page has JavaScript set to force the user to stay. Now, the client has no option but to let the web page install a virus.

If web browser authors did not account for this, we would have critical browser issues!

asifone
April 14th, 2009, 01:06 PM
Ok now i would clarify m actual requirements.

I have a page with some four fields which the user need to fill in and save his details when his visits for the first time and before navigating to any other page.
Now suppose the user is active and he comes to that page first time after activating..and here we want him to forcefully fill up all details before navigating to other page.

now user comes for first time...Page load is called and as the details are empty i have registered my script such that it will throw popup alert in unbeforeunload..i.e. when user is tryng to leave the page.

System.Text.StringBuilder script = new System.Text.StringBuilder();
script.Append("<script type='text/javascript'> ");
script.Append("AddEventHandler(window,'beforeunload', CheckSaved); ");
script.Append("function CheckSaved() { ");
script.Append(" if (showalert = true);");
script.Append("{");
script.Append("if(window.event)");
script.Append("{");
script.Append("alert('Please save before navigating');");
script.Append("return false;");
script.Append("}");
script.Append("else");
script.Append("{");
script.Append("alert('Please save before navigating');");
script.Append("return false;");
script.Append("}");
script.Append("}");
script.Append("}");
script.Append("</script>");
return script.ToString();


script may be bit wrong here and there but just get the logic.

Now as the details are empty i haveregistered the script on load and then as user tries to leave the page onunbeforeunload is called ..it will throw the Alert....good

Now suppose if user enters the details ..saves and then tries to leave..as the script is already registered on pageload or prerender ..it will again throw alert on onunbeforeunload ...saying please save before navigating .......how will i unregister it.
i dont want alert now..as user has saved the details...


Is there any option of registering the events on textboxeds..instead of any page events....if yes i am C# ..asp.net page eventd..if i am suppose to register events on controls..i.e.textboxes..where will i put it..on ascx ...or in acscx.cs...if .cs in which asp.net event ??

Please help me on this.


Thanks and Regards