Jump to content

Alert When Moving Out of Web Page


elabuwa

Recommended Posts

Hi Guys,

 

seen some pages who give alerts or Warnings mentioning that i'm moving away from my webpage. Best example is hotmail when you are on the compose mail screen and when click another link it is asking for confirmation.

 

Can you one please shed some light on how to do this.

 

Your help is greatly appreciated.

 

Cheers

Dileep

Link to comment
Share on other sites

there is no easy solution to this to my knowledge.. also can be pretty annoying to the people viewing your site so I really don't recommend it.. but if you have a good reason for doing it then you might want to look at the onunload event..

 

also to point you in the right direction.. http://stackoverflow.com/questions/2365994/display-a-warning-when-leaving-the-site-not-just-the-page

Link to comment
Share on other sites

You can do

window.onbeforeunload = function(){
    return 'are you sure you want to leave?';
}

this will lead to undesired results, ex. clicking an anchor that links to an internal page will trigger this.. since it looks for the DOM being unloaded only

Link to comment
Share on other sites

this will lead to undesired results, ex. clicking an anchor that links to an internal page will trigger this.. since it looks for the DOM being unloaded only

OP

Best example is hotmail when you are on the compose mail screen and when click another link it is asking for confirmation.

Dileep

Seems to me that this is exactly what the OP wants.

Link to comment
Share on other sites

Just a wild guess. Is it a registration form or adding contents? and you would like to give warning to the user that if he/she navigates away from page the data will not be save?

 

then you can use what nogray provided. and for undesired result you could add checking if the content has a content and fire the javascript:

I'm talking about like this:

<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
window.onbeforeunload = function(){
	var txt = document.getElementById("content").value; 
	if(txt != '') { 
		return 'are you sure you want to leave?';
	}
}
</script>
</head>

<body>
    Type content here: <input type="text" name="content" id="content">
</body>
</html>

 

 

Just a suggestion and wild guess.  :shrug:

cheers!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.