Delete Confirmation Prompt with Javascript and ASP

Asked

Viewed 857 times

2

I have a list of titles presented dynamically to the user:

[ x ] - [ + ] - Título 1
[ x ] - [ + ] - Título 2
[ x ] - [ + ] - Título 3
[ x ] - [ + ] - Título 4

The number of items is variable and the values for Title also depend on the user’s previous choices.

The first button [ X ] is for RULE OUT the record, the second [ + ] is for EDIT.

In the of RULE OUT I need a window of Prompt that CONFIRM the deletion of the record.

I did so: onClick="javascript:window.confirm('Deseja realmente excluir?')"

But the two buttons of the prompt do the same action. This is OK deletes and CANCEL also.

Does anyone have any idea how to make this work?

  • Helio, you have to put a piece of HTML here to understand it better. When you say "DELETE the record" you mean delete the set [ x ] - [ + ] - Título 1 clicked?

  • It’s not HTML, Beast, it’s ASP. E mounts the list with a loop, like: While NOT Rstemp.EOF Response.write "<p>" Response.write "<a href=delete.Asp? ID=" & Rstemp("id") Response.write " onClick="&Chr(34)&"javascript:window.confirm Response.write "('Do you really want to delete?')"&Chr(34)&" Response.write ">" Response.write "[ X ]</a> - " Response."write<a href=Edit.Asp? ID=" & Rstemp("id") & ">" Response.write "[ + ]</a> - " Response.write Rstemp("title") Response.write "<br>" Rstemp.Movenext Wend

  • I’m sorry, I don’t understand why the formatting I apply is eliminated. There’s this tile that even I can’t understand. What a turd...

  • You are using ASP to build HTML. In this particular case, I think you should use pure HTML since the event onclick is static and does not vary depending on certain situations.

3 answers

2

function cConfirm(str, link) {
    if (window.confirm(str)) {
        window.location.href = link;
    } else {
        // código que queres executar caso sepra pessionado CANCEL
    }
}

Then instead of calling directly window.confirm in your element, you use
onClick="javascript:cConfirm('Deseja realmente excluir?','?edit=true&id=1')"

explaining: the window.confirm returns a boolean when you press any of the buttons - returned true if OK or false in the event of CANCEL

    1. Would I have to apply this solution to each link? The list is dynamic! It may have ONE delete link or 3,400. 2) "Code I want to execute"... I don’t know how to write in javascript what I want it to do. That is, there is a link that the user clicked on at this point, I just wish it to go ahead and perform the action related to the chosen link or do nothing. (now I think I’ve made it worse...)
  • @Heliosouza depends on how you generate the pages.. basically; I don’t know ASP - I suppose you can change the arguments of the function to cConfirm(str, link) and in the true give away window.location.href = link

1


Use your event onclick as follows:

onclick="return confirm('Deseja realmente excluir?')"

When this event returns false, behavior is similar to calling event.preventDefault();

I don’t know ASP, but I did my research and found that to use double quotes on an ASP string, simply duplicate them:

Response.Write(" onclick=""return confirm('Deseja realmente excluir?')""")

In any case, I suggest leaving this part of the code outside the ASP by closing the tag before and reopening afterwards:

%> onclick="return confirm('Deseja realmente excluir?')"<%
  • Opa Wesley, beleza? I tried this alternative, unfortunately when written this way the click does not open the prompt. Maybe because of the way I tag: Response.write " onclick="&Chr(34)&"Return confirm('Confirm delete')"&Chr(34)&">" So on and so forth...

  • @Heliosouza, try to duplicate the quotes instead of using char(34): Response.Write(" onclick="""Return confirm('Confirm exclusion')"">")

  • @Heliosouza, or you can close the ASP tag and then reopen since the content is static: %> onclick="return confirm('Confirma exclusão?')"<%

0

Thanks guys,

I put together elements of the two answers.

From Moshmage the cConfirm function.

From w35l3y the hint about leaving the call to function OUTSIDE of the ASP block and the treatment of the event in case of FALSE.

 <script type="text/javascript">
     function cConfirm(str, lnk) {
        if (window.confirm(str)) {
            window.location.href = lnk;
        } else {
            event.preventDefault();
        }
    }
</script> 

And the call to duty was:

        %>
        onClick="javascript:cConfirm('Deseja realmente excluir?','delete.asp?pagina=<%=pag%>&NID=<%=RStemp("news_id")%>')">
        <%

I appreciate your patience and your willingness to help.

Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.