• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5861
  • Last Modified:

Javascript Yes/No Alert Box

Experts,

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}
}

Regards
easynow
0
Dean OBrien
Asked:
Dean OBrien
  • 3
  • 3
  • 2
  • +2
1 Solution
 
copyPasteGhostCommented:
0
 
Ray PaseurCommented:
Easynow111: In PHP I use this "error halt" script to deal with incorrect passwords:
function error_halt($message) {
?>
      <html><head>
      <script language="JavaScript">
      <!--
            alert("<?=$message?>");
            history.back();
      //-->
      </script></head><body></body></html>
<? exit;
}

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

Best regards, ~Ray
0
 
bakuretsuCommented:
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

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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 in....my ignorance though :)

Cheers
Easynow
0
 
ruscompCommented:
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;}
0
 
bakuretsuCommented:
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.
0
 
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.
Cheers
Easynow
0
 
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:

http://webaccelerator.google.com/webmasterhelp.html#prefetch7
0
 
Dean OBrienAuthor Commented:
Hi Ray,

Very valid suggestion, I take it on board thanks.

Easynow
0
 
bakuretsuCommented:
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');
    f.style.display = 'none';
    this.parentNode.appendChild(f);
    f.method = 'post';
    f.action = this.href;
    f.submit();
    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.
0
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.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now