Create a JavaScript Confirmation Box

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

Comments

  1. blogywalkie.com

    it is really cool!

  2. netmastan

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

  3. Chris

    @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

    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

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

  6. PHPSUCK

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


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: