Create a JavaScript Confirmation Box

Written by David Walsh on December 28, 2007 · 6 Comments

Javascript confirmation boxes aren't used nearly as often as they used to be, mostly due to more advanced usages of JavaScript and modal boxes, but they are still helpful. Whenever I create administrative functionality for my customers, I always use a JavaScript confirmation box before executing any permanent action -- an example would be when a customer tries to delete a file.

The Code / Example

Delete File

The confirm method returns the true/false result of the confirm box.

Comments

  1. it is really cool!

  2. netmastan January 26, 2009

    doesn’t work on FF. If you hit cancel still goes to the link

  3. @netmastan

    Try adding a ‘return false’ statement at the end.

    eg. 1. <a href="http://domain.com/admin/delete-file.php?file_id=4" onclick="return confirm('Are you sure you want to delete this file?'); return false;">Delete File</a>

  4. clikcspeed March 24, 2009

    You rock! This is great, was just looking for a way to pass PHP variable to javascript but with this tutorial I don’t have to complicate things, the whole process becomes less cumbersome… Really great stuff man!!!

  5. toinkees May 14, 2009

    this helped me have an idea… thanks guys!!!..

  6. var c=confirm(“Are u sure?”);
    if(c== false)
    {return false;}
    else
    return true;

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
CSS3 and Your Browser
Newer
PHP Redirect Function