Javascript Yes/No Alert Box


I would like to add a barrier between someone clicking on a link and the leaving the page
(i.e. are you sure you want to go to this url [in my case execute a delete to DB])

Im aware i would need to call a function similar to this, but am a little hazy on the syntax.
function action(url)
result = alert();
if(result==Y) {goto url}
else {do nothing}

LVL 12
Dean OBrienAsked:
Who is Participating?
copyPasteGhostConnect With a Mentor Commented:
Ray PaseurCommented:
Easynow111: In PHP I use this "error halt" script to deal with incorrect passwords:
function error_halt($message) {
      <script language="JavaScript">
<? exit;

The "history.back();" part may be useful to you in the "do nothing" case.

Best regards, ~Ray
Try this instead. It's not yes/no, it's OK/Cancel, but the same idea.
if(confirm("Are you sure?")) {
  // goto url, or other action

Open in new window

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Dean OBrienAuthor Commented:
Thanks for comments guys, copypasteghost's solution hits the nauil on the head.
Ray, i prefer to deal with it client side, appears simpler approach. Plus my php hat could figure out exactly how it would fit ignorance though :)

returning false on the link should prevent the link from taking the user to it's href.

Note: There is no yes/no box in javascript (unless you make one), only the Ok/Cancel box using the confirm function.

Confirm returns true when they hit Ok, and false when they hit cancel...

So if they hit cancel, return false, which should prevent the link from following through.

if(confirm("Delete Database?")=false){return false;}
You can do it directly within a link, as well, which frameworks such as Ruby on Rails do when you want to have a delete action confirmed by the user.

<a href="/a/path/that/will/delete/something" onclick="return confirm('Are you sure?');">Delete!</a>

If you click Cancel, nothing happens because the onclick="" returns false. If you click OK, the browser will proceed to the href location.
Dean OBrienAuthor Commented:
Cheers for the additonal comments guys,
<a href="/a/path/that/will/delete/something" onclick="return confirm('Are you sure?');">
Is the exact solution i was looking for.....

Sorry but ghost's post came in first.
Ray PaseurCommented:
Normally I would no longer have anything to add to this discussion, but you might want to be aware of this issue and code defensively.  To sum up, NEVER have a link that can change the state of your web site that does not pass its parameters via POST:
Dean OBrienAuthor Commented:
Hi Ray,

Very valid suggestion, I take it on board thanks.

Ray Paseur is totally correct. You can get around this with more JavaScript trickery that e.g. Rails uses to make a link that posts a form. It's pretty simple:

<a href="/url/that/deletes/1234" onclick="
    var f = document.createElement('form'); = 'none';
    f.method = 'post';
    f.action = this.href;
    return false;
">Delete something!</a>

Now, that works great for Rails because GET and POST variables are handled identically, so if you wanted to be able to specify other specific POST variables, you'd have to write more JavaScript (e.g. to create hidden inputs inside the form, etc.)

The only reason to do this is if you desperately need to have a link instead of a form button. If you can use a button, just create a regular form.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.