Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Asking Users To Confirm If They Wish To Leave The Page


15 replies to this topic

#1 dyknight

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 32 posts

Posted 05 July 2006 - 12:51 PM

I am sure all of us have had the frustration of having to retype an entire email from scratch because we accidentally hit "back" or otherwise left the page. As web technology improves and becomes more profound, such problems have found their simple solutions, as displayed by Google on Gmail, Google Calendars, Google Pages, Blogger etc. This tutorial will explore this script used to prevent a user from leaving the page accidentally.

In order to achieve this, make sure your users have Javascript enabled. We will be using window.onbeforeunload.

Firstly, open up your HTML in your favorite text editor, such as Notepad or Dreamweaver. You will see something like:

<html>
....
<head>
....
</head>
<body>
...
</body>
</html>

Next, paste the following into the script, between the head tags:

<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
		return "You have unsaved changes.";
		}
</script>

so that we get this:

<html>
<head>
<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
		return "You have unsaved changes.";
		}
</script>
</head>
<body>
</body>
</html>

Save the script and test it in your browser. Try navigating away from the page. A popup pane will appear, asking you to confirm your exit and showing the message: "You have unsaved changes.";
Of course, we would like to achieve more than that, such as asking only when the user has made changes to the form.

We will need to do this:

<script language="javascript" type="text/javascript">
	needToConfirm = false;
	window.onbeforeunload = askConfirm;
	
	function askConfirm(){
		if (needToConfirm){
			return "You have unsaved changes.";
			}	
		}
</script>

We first set a boolean "needToConfirm" to false, so that we don't ask unless the user has done something, and this something will change needToConfirm to true. Before unload, the function askConfirm will be called. If the boolean "needToConfirm" is true, the function will return a string and there will be a pop-up window. Else, it will return null and the pop-up won't show. Now we need to add something to set needToConfirm to true.

<form>
	<input type="text" onchange="needToConfirm=true"/>
	<input type="submit" value="Submit"/>
</form>

The input field, when changed, will set needToConfirm to true. If unchanged, the boolean will be false. However, we do want to note that when the user has changed and SUBMITTED the form, we do not show the pop-up. So we add the following:

<form onsubmit="needToConfirm=false;">
	<input type="text" onchange="needToConfirm=true"/>
	<input type="submit" value="Submit" onclick="needToConfirm=false;" />
</form>

And we result in the final code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script language="javascript" type="text/javascript">
	needToConfirm = false;
	window.onbeforeunload = askConfirm;
	
	function askConfirm(){
		if (needToConfirm){
			return "You have unsaved changes.";
			}	
		}
</script>

</head>

<body>

<form onsubmit="needToConfirm=false;">
	<input type="text" onchange="needToConfirm=true"/>
	<input type="submit" value="Submit" onclick="needToConfirm=false;" />
</form>

</body>
</html>

There you go. You might want to save the javascript in an external file and link it, so that you don't have to enter the code on every page. Use the following:

<script language="javascript" type="text/javascript" src="[i]your file name[/i]>


#2 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 06 July 2006 - 03:48 AM

Nice code, but since it requires use of Javascript, it won't work if a user has js disabled.

#3 Albus Dumbledore

    Nothing ventured, nothing gianed

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1,563 posts
  • Gender:Male
  • Location:Sacramento California
  • Interests:Allot of things, websites, coding, photography, my future
  • myCENT:73.26

Posted 06 July 2006 - 04:42 AM

yeah, ive seen simpler codes than this before, and as jlhaslip said, if they have Javascript disbled, it wont show anything, and sometimes that can just get soo annoying!

*closes page*
POPU-UP
Are you posative you want to leave this page?
*sighs*
*clicks yes* i said i was!!!!

hehe

#4 SolarX

    Member [Level 3]

  • Kontributors
  • PipPipPipPipPipPip
  • 90 posts
  • Location:our universe
  • Interests:movies, pc games, webdesign

Posted 07 July 2006 - 12:18 PM

Hm, I'm using Opera and I don't have such problems, cause Opera saves the text you typed in a form or field automatically. So if you hit back accidentally and forward again nothing is lost.

But it's a good addition four IE users...

#5 FirefoxRocks

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 274 posts
  • Gender:Male
  • Location:Ontario, Canada, North America, Planet Earth
  • myCENT:88.65

Posted 07 July 2006 - 01:15 PM

If you read the W3Schools page, they do say that JavaScript is the scripting language on the Internet.

Opera and some extensions on the Firefox browser save form fields so they don't get cleared (well on some websites). That won't be a problem for those users.

It is a good addition for IE visitors to your site.

#6 dyknight

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 32 posts

Posted 08 July 2006 - 02:22 AM

Yup, Javascript is the client-side scripting language of the Internet. What I would do is to add a short notice on the frontpage to warn the user that he should have javascipt enabled. Without Javascipt, site functions and flexibility are pretty limited.

Anyway, for the more experienced, you can add more checks to ensure that you don't pop-up the window unnecessarily. For my example I added a check for submission.

Edited by dyknight, 08 July 2006 - 02:23 AM.


#7 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 24 August 2009 - 07:28 AM

Nice work, nice code!Asking Users To Confirm If They Wish To Leave The Page

Thank you for great script. It´s quite easy and simple, but works great! I hope it helps me with my eshop to ask my customers why (if it´s true) they haven´t bought something.

-reply by Mario Herak

 



#8 Spurious

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 19 posts

Posted 24 August 2009 - 09:17 PM

Quote

Nice code, but since it requires use of Javascript, it won't work if a user has js disabled.
Ive been looking for a non-Javascript code but still not found one. When I first looked at it, it didn't look like js but it does now.

#9 damoon

    Newbie [Level 3]

  • Kontributors
  • PipPipPip
  • 45 posts
  • Gender:Male
  • Location:Nigeria
  • myCENT:29.05

Posted 28 September 2009 - 10:29 AM

javasript is too irrational.....i love PHP better but thanks anyways

#10 mahesh2k

    Trap Double Mocha Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,347 posts
  • Gender:Male
  • Location:Valley of Darkness
  • myCENT:27.17
  • Spam Patrol

Posted 28 September 2009 - 11:12 AM

Quote

javasript is too irrational.....i love PHP better but thanks anyways

Javascript is irrational ? ? :lol: How is that ? Javascript and PHP is comparable ? I don't think so.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users