Solved

Javascript Yes/No Alert Box

Posted on 2008-06-26
10
5,847 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

809 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