|
|
Home » Javascript » Article
Confirming a Delete using Javascript Confirm
|
| Article by: | Jeff Anderson ( 1362 ) (3/29/2002) |
|
| Summary: | If you have a page which deletes records from a database, or indeed have any kind of link that you want the user to be doubly certain that they 'mean what they click', using the Javascript confirm event handler is a simple and effective way to get the job done. |
|
| Viewed: 88680 times |
Rating (3 votes): |
|
5 out of 5 |
|
|
|
Confirming a Delete using Javascript Confirm
If you have a page which deletes records from a database, or indeed have any kind of link that you want the user to be doubly certain that they 'mean what they click', using the Javascript confirm event handler is a simple and effective way to get the job done. Here's the effect we're going to achieve:
Click here to return home
Assuming you're still with us and didn't after all return to the home page, you can see that the confirmation makes doubley sure you're aware of what you're doing. (Apparently some sites also use it for asking 'Are you over 18'...)
Now let's look at the script:
Keep all the above on one long line to ensure it works properly.
The key to this code is the phrase return confirm. Just putting confirm by itself wouldn't do much, we have to tell the browser what to do with it's confirmation. Once we tell it to return the users selection (either true for Yes or False for No), the code now simply returns false -- does nothing -- if a No is selected, and if it's true -- a Yes response -- completes the link command and zooms off to the delete page or whatever - Magic!
|
|
View highlighted Comments
User Comments on 'Confirming a Delete using Javascript Confirm'
|
Posted by :
Archive Import (Matthew Dull) at 01:12 on Thursday, April 03, 2003
|
I needed a javascript event handler to catch my users from accidentally deleting articles from my MySQL database. But the articles' ID's were getting dynamically generated by a PHP loop, so i needed the script all on one line (inside the link, not in an outside function) so each script's action was customized. This is exactly what i needed.
| |
|
|
Posted by :
Archive Import (Jeff) at 14:10 on Friday, April 11, 2003
|
I also had a looping function that added on ID #'s, and needed a quick confirmation... and this worked perfectly!
| |
Posted by :
Archive Import (Natasha) at 11:55 on Thursday, June 05, 2003
|
Thank you. I'll make sure to bookmark this page.
| |
Posted by :
Archive Import (Jennifer) at 12:42 on Thursday, July 17, 2003
|
I am having the problem where even when I click cancel on the popup confirm, it still does the action. I notice your example on this webpage does it too (still takes me to the home page when I select "Cancel"). Has anyone else noticed this?
Thanks
| |
Posted by :
Archive Import (Velcro) at 11:48 on Monday, July 21, 2003
|
I used this with a Delete button and it worked perfectly. Just what I was looking for!!!!
<input type="submit" name="cmdSubmit" value=Delete onclick="javascript:return confirm('Are you sure you want to delete this POC?')">
| |
Posted by :
Archive Import (Baub) at 09:33 on Monday, August 25, 2003
|
Great! Saved me a lot of time. Thank you! Thank you! Thank you!
| |
|
|
Posted by :
AppleCoder at 18:09 on Friday, February 23, 2007
|
I know this is from an old article, but I am looking to find a way to change the pop up window text.
Instead of the URL of the site, I want to display certain text, can that be done?
This is what is normal
------------------------------
- http://www.codetoad.com -
- -
- You want to leave the site? -
- -
- (cancel) (ok) -
------------------------------
I want this
------------------------------
- Aria Pictures -
- -
- You want to leave the site? -
- -
- (cancel) (ok) -
------------------------------
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
Javascript - Enable and Disable form elements by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form. |
 |
Check IsNumeric Function by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric. |
 |
Javascript Onload Event by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy |
 |
Form Validation Function by Jeff Anderson
A javascript validation function that you can use to validate all types of forms. |
 |
JavaScript Field Is Empty Form Validation by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not. |
 |
Javascript Get Selected Text by Jeff Anderson
A cross-browser script to get text selected by the user |
 |
Check Email Validation Function by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form. |
 |
Javascript OnMouseDown by Jeff Anderson
The javascript event handler that is called when you click the mouse button. |
 |
Validate Form and Disable Submit Button by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating. |
 |
Multiple submit buttons on a single form by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button. |
 |
| |