|
|
|
|
![]() ![]() |
Jul 5 2006, 12:51 PM
Post
#1
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 32 Joined: 30-June 06 Member No.: 25,862 |
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: CODE <html> .... <head> .... </head> <body> ... </body> </html> Next, paste the following into the script, between the head tags: CODE <script language="javascript" type="text/javascript"> window.onbeforeunload = askConfirm; function askConfirm(){ return "You have unsaved changes."; } </script> so that we get this: CODE <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: CODE <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. CODE <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: CODE <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: 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: CODE <script language="javascript" type="text/javascript" src="[i]your file name[/i]>
|
|
|
|
Jul 6 2006, 03:48 AM
Post
#2
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,993 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
Nice code, but since it requires use of Javascript, it won't work if a user has js disabled.
|
|
|
|
Jul 6 2006, 04:42 AM
Post
#3
|
|
|
Hidden Secrets can't be told threw just words. One must feel what the other feels to truely understand... ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 1,523 Joined: 8-January 06 From: Sacramento California Member No.: 16,756 |
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 |
|
|
|
Jul 7 2006, 12:18 PM
Post
#4
|
|
|
Member [Level 3] ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 90 Joined: 5-January 06 From: our universe Member No.: 16,648 |
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... |
|
|
|
Jul 7 2006, 01:15 PM
Post
#5
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 273 Joined: 14-April 06 From: Ontario, Canada, North America, Planet Earth Member No.: 21,845 |
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. |
|
|
|
Jul 8 2006, 02:22 AM
Post
#6
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 32 Joined: 30-June 06 Member No.: 25,862 |
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. This post has been edited by dyknight: Jul 8 2006, 02:23 AM |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 6th September 2008 - 01:48 AM |