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
https://forums.phpfreaks.com/topic/249970-alert-when-moving-out-of-web-page/
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

  Quote

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

  Quote

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

  Quote

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.

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!

Archived

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

×
×
  • 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.