9

I'm using simple links for an admin panel but in case of user accidently clicks on link for user deletion, there should be a confirmation pop-up for avoding this.

Probably as you know a href tag's are not fully compatible with javascript. So, i have used span tag for "onclick" but that didn't went so well:

<script type="text/javascript">
function confirm_click()
{
return confirm("Are you sure ?");
}

</script>

Here is the html code for link:

   <span onclick="confirm_click();">    <a title="Delete User" href="http://www.google.com"><i class="icon-remove-sign">DELETE</i></a></span>
mirza
  • 5,685
  • 10
  • 43
  • 73

5 Answers5

17

Try...

<a title="Delete User" onclick="return confirm_click();" href="http://www.google.com"><i class="icon-remove-sign">DELETE</i></a>

...instead.

You need to return the value of your method, and returning false in an onclick event prevents the default behaviour (which in this case would be to navigate to the href).

MadSkunk
  • 3,309
  • 2
  • 32
  • 49
12

Try

<a href='/path/to/action' onclick="return confirm('Are you sure ?')">Delete</a>
lostsource
  • 21,070
  • 8
  • 66
  • 88
3

Here is my code:

<a title="Delete User" id="delete" href="http://www.google.com"><i class="icon-remove-sign">DELETE</i></a>
<script type="text/javascript">
document.getElementById('delete').onclick = function(e){
    if( !confirm('Are you sure?') ) {
        e.preventDefault();
    }
}
</script>

and I think we avoid using 'onclick' and so on in HTML in order to make HTML and JavaScript separate

Rex Huang
  • 85
  • 4
0

This javascript will do the job

function confirmDelete(delUrl) 
    {
        if (confirm("Sample message")) 
        {
            document.location = delUrl;
        }
    }

Obviously you have to assing it to

<a href="javascript:confirmDelete('yourPath')">Delete</a>
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
0
<body onunload="return confirm_click()">

When the page is going to be unloaded your confirm message is prompted

A resource about unload event: http://www.w3schools.com/jsref/event_onunload.asp

SaidbakR
  • 13,303
  • 20
  • 101
  • 195