Solved

Javascript Yes/No Alert Box

Posted on 2008-06-26
10
5,848 Views
Last Modified: 2010-04-21
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
Comment
Question by:Dean OBrien
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 125 total points
ID: 21873368
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 21873532
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
 
LVL 1

Expert Comment

by:bakuretsu
ID: 21873859
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 12

Author Closing Comment

by:Dean OBrien
ID: 31470916
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
 
LVL 7

Expert Comment

by:ruscomp
ID: 21874082
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
 
LVL 1

Expert Comment

by:bakuretsu
ID: 21874132
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
 
LVL 12

Author Comment

by:Dean OBrien
ID: 21874185
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 21874273
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
 
LVL 12

Author Comment

by:Dean OBrien
ID: 21882212
Hi Ray,

Very valid suggestion, I take it on board thanks.

Easynow
0
 
LVL 1

Expert Comment

by:bakuretsu
ID: 21883032
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question