Solved

Javascript Yes/No Alert Box

Posted on 2008-06-26
10
5,845 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
Comment Utility
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
 
LVL 12

Author Closing Comment

by:Dean OBrien
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:bakuretsu
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Hi Ray,

Very valid suggestion, I take it on board thanks.

Easynow
0
 
LVL 1

Expert Comment

by:bakuretsu
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now