Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to stop navigation to another page using javascript

Status
Not open for further replies.

anayyar

Programmer
Apr 18, 2002
1
IN
I have to give the user a user-defined message when he/she moves out of the current page as whether he/she wants to save or not. If it wants to save then I should save the current form and move ahead. But if it wishes to save but some validation fails then it should give an error message for validation and stays on the current page itself. If it doesn't wants to save then I don't need to do anything.

I have tried using window.event.returnValue="" in onbeforeunload event but it doesn't allow me to give custom message and it allows only to go to the new page or stay but doesn't allow to perform action of my choice.

If anybody has some idea regarding this please help me.

Waiting for your responses....
 
Hi

It depends on how they leave. If you mean when they click a link/image on the page then there is a solution:

On the link do this:

<a href=&quot;javascript:eek:penPage('newpage.htm')&quot; ...

Then use a function to navigate:

function openPage(url){
if(!changed){
window.location = url;
}else{
alert(&quot;You have made changes&quot;);
....


You will need a flag variable called change initially set to false. If a user changes a field on your page you need to trigger an &quot;onchange&quot; event and set changed=true.

I can provide further assistance on the details of handling this - just let me know that I have understood you before I write tons of code ;-) I use something similar to detect changes and prompt for a save if a user changes something on a form.

Cheers,
Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top