Prevent a visitor from navigating away from curent page

kansun
kansun used Ask the Experts™
on
Hello and good day,

I have a web page with a html form in it.
The form has some mandatory fields too with submit button.
Now this is what i wanna achieve...
If a visitor arrives on this form page and tries to leave the page without submitting the form (either by typing a new url in address bar or by clicking back button) then the visitor should be prevented from navigating away from the form by displaying a small css html window (NOT the typical Javascript alert) with a message asking if visitor needs any assistance with a text area and 2 buttons (Yes button and Navigate away button). If he presses yes button then all that he types in the textarea will be submitted to website admin and if he wishes to navigate away from page he can by clicking on Navigate away button.

Can this be done?
If yes can you please upload a complete working code (a basic code at least)
Thanks
kansun
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
>Can this be done?

Not really.
Do you want something working 100% of time?
Darrell PorterEnterprise Business Process Architect
Commented:
<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }
</script>

from

http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm?name=&age=

Modify the function to call the render the appropriate window
leakim971Multitechnician
Top Expert 2014

Commented:
@WalkaboutTigger :

small css html window (NOT the typical Javascript alert)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

leakim971Multitechnician
Top Expert 2014

Commented:
additionaly onbeforeunload don't work on all browsers.
Commented:
you can only use the traditional javascript modal dialog - you can't customize it.  the event is window.onbeforeunload, which is managed by the browser (and there's nothing you can do about it), but is the only thing that can interrupt a page exit.

here's a little info on it:  http://upshots.org/javascript/window-onbeforeunload
It's the worst user experience you can provide!!
Do the best you can to attract visitors, but don't try to prevent them from navigating away!
Top Expert 2007

Commented:
You cannot force the user to do anything. If you make it difficult to leave, my guess is they will not be back.

Author

Commented:
Hi My friends,
I expected a solution. But i just got few advices here.
:-)) Can anyone provide a soln for this.... thx
Top Expert 2007
Commented:
>>I expected a solution.

You cannot prevent the user from leaving your page. Worst case, she will close the browser window and never return to your site.

You can train your users on the proper way to exit your page, and explain the consequences for not doing so.
Commented:
Badotz is correct.  Sometimes the answer is that there is no solution.

You can only interrupt page exit using window.onbeforeunload.  Doing so will result in a browser-generated modal dialog.  This cannot be replaced with an HTML fragment.

There's no other answer.
Top Expert 2007

Commented:
And onbeforeunload may not be supported in all browsers.

Author

Commented:
I've requested that this question be deleted for the following reason:

no answers
Top Expert 2007

Commented:
The answer is, "You cannot prevent the user from leaving."

What more do you want?

leakim971Multitechnician
Top Expert 2014

Commented:
99% of time for this question the member want a << solution >> not a << right answer >>...


Top Expert 2007

Commented:
<waiting_for_the_envelope>
...and the solution is...
</waiting_for_the_envelope>

Author

Commented:
thank you
Darrell PorterEnterprise Business Process Architect

Commented:
There are no US-legal solutions to your question, unless you control the machine from which the user is browsing (like a kiosk machine).  I presumed from your question, however, that this was not the case.
Top Expert 2007

Commented:
>>unless you control the machine from which the user is browsing (like a kiosk machine)

So, what is the "solution" in that case?

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