Display confirmation popup with JavaScript upon clicking on a link

How do I make one of those hyperlinks where when you click it, it will display a popup asking "are you sure?"

<INPUT TYPE="Button" NAME="confirm" VALUE="???" onClick="message()">

I already have a message() function working. I just need to know what the input type for a hyperlink would be.

-------------Problems Reply------------

<a href="http://somewhere_else" onclick="return confirm()">

When the user clicks the link, the confirm function will be called. If the confirm function returns false, the link traversal is cancelled, if true is returned, the link is traversed.

<a href="http://something.com" onclick="return confirmAction()">try to click, I dare you</a>

with the function

function confirmAction(){
var confirmed = confirm("Are you sure? This will remove this entry forever.");
return confirmed;
}

(you can also return the confirm right away, I separated it for the sake of readability)

Tested in FF, Chrome and IE

As Nahom said, except I would put the javascript:message() call directly in the href part (no need for onclik then).

Note: leaving the JavaScript call in the onClick has a benefit: in the href attribute, you can put a URL to go to if the user doesn't have JavaScript enabled. That way, if they do have JS, your code gets run. If they don't, they go somewhere where they are instructed to enable it (perhaps).

Now, your message routine must not only ask the question, but also use the answer: if positive, it must call submit() on the form to post the form. You can pass this in the call to ease the fetching of the form.

Personally, I would go for a button (input tag as you show) instead of a simple link to do the process: it would use a more familiar paradigm for the users.

[EDIT] Since I prefer to verify answers I give, I wrote a simple test:

<script type="text/javascript" language="JavaScript">
function AskAndSubmit(t)
{
var answer = confirm("Are you sure you want to do this?");
if (answer)
{
t.form.submit();
}
}
</script>

<form action="Tests/Test.html" method="GET" name="subscriberAddForm">
<input type="hidden" name="locationId" value="2721"/>
<input type="text" name="text" value="3.1415926535897732384"/>
<input type="button" name="Confirm" value="Submit this form" onclick="AskAndSubmit(this)"/>
</form>

Yes, the submit just reload the page here... Tested only in FF3.

[EDIT] Followed suggestion in the comments... :-)

<a href="#" onclick="message(); return false;">???</a>

This answer would be OK only when the click need NOT navigate the user to another page.

Category:javascript Views:3 Time:2008-11-16

Related post

  • Javascript to click on href link 2012-01-18

    I got this code after searching around on stack overflow and it should work, but for some reason I am getting an error. I am trying to write JS that simulates a user clicking on a specific href link that has a specific ID. This needs to work in all t

  • display confirmation message after a button click inside update panel 2011-08-03

    In my page, I have 3 update panels. each of them have a save button. I need to show a confirmation message after the system saves data successfully. The message must show and fade out after 5 seconds. I have a DIV and inside that I have a label on wh

  • "Navigation Cancelled" displays multiple times(15-20) when clicking on "mailto" link in IE 10, Lotus Notes default email 2015-02-13

    When I am on a website, and I click on a mailto: link, I immediately have 15 to 20 instances/windows display the message "Navigation Cancelled" and then an additional message telling me that my email client is not installed properly (It is, and it wo

  • Display modal popup with some text after click on Hyperlink 2010-11-03

    How can I can display modal popup with some text clicking on Hyperlink. I would link use Ajax with ASP.net 4.0. I am new to ASP.NET 4.0. Please advise! Thanks.. --------------Solutions------------- This should do the trick http://dev.iceburg.net/jque

  • how do i have dynamic confirmation popup using simple modal 2009-08-06

    I am using simple model which is a very neat piece of code but i have one requirement i can't figure out. http://www.ericmmartin.com/simplemodal/ my use case is the third options where i want a "Confirmation Popup" after a user clicks on an action. T

  • Internet Explorer 9 opens a blank page when clicked on a link in the current document. 2015-01-25

    Whether I am clicking on a story in Net Zero's home page or a link in an e-mail with my Clear.net e-mail, or clicking on a link on your website, I can't link from the current document. I have a two week old Dell Inspiron computer running Window's 7.

  • How to get javascript confirm popup value in c#.net 2013-06-14

    I want to get javascript confirm popup returns value from code behind. Here when user select ok button on confirm popup some code goes here, or user select cancel button on the popup some code goes here. How to get the selected value from code behind

  • Javascript Confirm popup Yes, No button instead of OK and Cancel 2009-05-05

    Javascript Confirm popup, I want to show Yes, No button instead of OK and Cancel. I have used this vbscript code: <script language="javascript"> function window.confirm(str) { execScript('n = msgbox("' + str + '","4132")', "vbscript"); return (

  • check if user activated their account via email and display message after clicking on activation link PHP/JavaScript 2011-01-24

    I'm working with the Zend Framework on a project that requires the user to activate their account by checking their email and clicking on the activation link. The activation link works fine and the user can login once their account is set active. My

  • Display a popup on long click in extended EditText class 2015-01-11

    I try to write a derived class of EditText for use on with dates public class myEditDate extends EditText { protected Context mContext; // Current activity ... It work fine and i add some utils date functions but i try to display a popup windows with

  • How to do form validation first and then a confirmation popup call in wicket 2011-12-14

    I am using ajax button and overridding the IAjaxCallDecorator getAjaxCallDecorator for javascript confirmation popup . when i click on button it calls this confirmation popup first then it does the form validation Do we have any way to first do the v

  • Jquery displaying confirmation dialog on form submit 2009-09-11

    I need to display confirmation dialog on image click. Code in "onclick" section of image should be only executed if jquery confirmation dialog returned true. At this point when user clicks on $(".submit_image") - dialog is displayed, but form is stil

  • Can we create onclick popup without Javascript in CSS? 2009-11-17

    Can we create onclick popup without Javascript in CSS? --------------Solutions------------- No, you can't. You have to use script to wire an event handler for onlcick. <a href="Somepage.html" target="_blank">Click me</a> and the user agen

  • Popup close on button click and redirect to different page! 2010-10-21

    I have a popup that is shown after successfully saving in the database. It shows "Password is successfully saved" and also has a "Ok" button. I want the popup to be closed when Ok is clicked and should be redirected to login page. With my code here,

  • How to display Jquery dialogbox on asp:Button click 2011-03-11

    I am new to JQuery and trying to display a Yes/No confirmation dialog box when the user clicks on an aspx button. When the dialog box gets displayed to the user he can click the Yes or No button and depending upon the user action i want to execute di

  • Display text when user clicks on any link 2011-04-24

    I want to display my licence agreement when the user clicks on any link on the page and prevent the link from taking the user anywhere. I need a way to make any click on the page show the code I want. Is there an easy way to do it? Like a scripts tha

  • Jquery Modal Dialog displaying MVC3 partial view - works first click only 2011-06-09

    public ActionResult MeanQ(int id) { Access access= db.Access.Find(id); return PartialView("_MeanQPartial", access); } The partial view thats being rendered in the above code is displayed in a Dialog Modal (Jquery)...The link(onclick) that displays th

  • How do I display a popup from a WebBrowser in another window I created? 2011-06-24

    I am trying to implement a simple web browser control in one of my apps. This is to help integrate a web app into a toolset i am creating. The problem is, this web app absolutly loves popup windows.... When a popup is opened, it opens in an IE window

  • Help with closing popup div javascript 2011-08-15

    Can i get help correcting the code below? You can just copy and paste and try it yourself. Onmouseover the popup div appears. If i click(X) the popup div should close but it doesn't. Only doubleclicking (X) closes the popup div. Onmouseover it should

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.209 (s). 11 q(s)