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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confirm on browser window close

Status
Not open for further replies.

curt62

Programmer
Feb 13, 2003
12
US
I am looking for a way to throw a confirm to save changes when a user attempts to close a browser window by hitting the X. I don't think I can use the unload event because that triggers when users navigate away as well as close the window.

Any ideas?
 
there's no way in js to detect the actual clicking of the "X".

you can prevent the window from closing in IE using "onbeforeunload", but you need to modify all other nav links to not trigger it on normal navigation.
Code:
<html>
	<head>
		<script language=&quot;javascript&quot;>
			var ok = false;

			function okToClose() {
				if (!ok) return &quot;Save Changes?&quot; + 
					&quot;\nClick OK to close without saving, or&quot; + 
					&quot;\nClick Cancel, then save your changes.&quot;;
			}
		</script>
	</head>

	<body onbeforeunload=&quot;return okToClose();&quot;>
		<a href=&quot;otherPage.html&quot; onclick=&quot;ok=true;&quot;>ok</a>
	</body>
</html>
=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top