Alert with two options(proceed or back to previous page)


<script language="JavaScript" type="text/javascript">
if (confirm("Are you sure to delete?"))
{
self.location = 'delete.php';
} else {
history.go(-1);
}
</script>

The above code give an alert with two choices(confirm or cancel).
If the user clicks the cancel button, it will go to previous page.
If the user clicks the confirm button, it will go to delete.php.

I like to make it like the following.
If the user clicks the cancel button, it will go to previous page.
If the user clicks the confirm button, It will proceed reading the next code.(the next code contains the code for deleting.)

I tried to remove “the self.location = ‘delete.php’;” as a test.


<script language="JavaScript" type="text/javascript">
if (confirm("Are you sure to delete?"))
{

} else {
history.go(-1);
}
</script>

The above code do;
If the user clicks the cancel button, it will go to previous page.
If the user clicks the confirm button, it will proceed reading the next code. So the deleting will be done.

It seems work fine.

But if the user clicks the cancel button, It will go to prevous page and the deleting will be done.(it might proceed reading the next code)

Can I have an alert with two options?
one is proceed reading the next code and the other is back to previous page(not reading the next code)?

I think that this is what your’re looking for:

<head>
  <script language="JavaScript" type="text/javascript">
  <!--
	function confirmDelete(loc) { 
	  if (confirm('Confirm you wish to delete this? ')) { 
		window.location.href = loc; 
	   } 
	  return false; // cancel the click event always 
	}
  //-->
  </script>
</head>
<body>
  <a href="delete.php" onclick="return confirmDelete(this.href);">Delete</a>
</body>

I can’t take credit for it as I copied it from a mate’s page, but I think it’s a neat solution and can explain it for you if necessary.

Andy

Andy,

I cleaned your function up a little:


<head>
  <script language="JavaScript" type="text/javascript">
  <!--
function confirmDelete() {
  return confirm('Confirm you wish to delete this? '))
  //location.href code not needed. If the confirm is
  //true (user clicks "OK"), they will go to the link anyway.
  //if user clicks "Cancel", link won't do anything since
  //confirm will return false.
}
  //-->
  </script>
</head>
<body>
  <a href="delete.php" onclick="return confirmDelete();">Delete</a>
</body>

Cheers!

I think that the original code was specifying a URL such as “delete.php?itemToDelete=0123&foo=bar” and using parts of the URL for things in addition to a simple redirect, hence the reason for passing the string.

You’re absolutely right though, it is overkill in this instance :slight_smile:

Andy

Just spotted a syntax error.
The function should read as follows:

  return confirm('Confirm you wish to delete this? ');

Andy

Ah!!
Well, just modifying the original code by joon, here’s what will do, through script only.


<script language="JavaScript" type="text/javascript">
var checK = confirm("Are you sure to delete?");
if (!checK)
{
history.go(-1); // go back since user clicked cancel
}
//rest of code to follow to delete whatever you want 
//or just add the else{} section to redirect to another
//page
</script>

I think this is what he wanted. He said that he want to execute the code that follows the confirm box if the user clicks OK else go back if he clicks CANCEL.

I don’t want any link on this issue.
It should be automatically executed when the page is open.

Then look at the script that I posted above.

Which one, please?

I posted another new one on this issue in this javascript forum.
It’s titled "make cancel button not to delete the record "
My problem is on there.

Would you answer me on there if you understand what I want and if you happen to know the solution?

As it happens, before this post, I’ve posted only twice in this thread & only one of them(also can be called, my first post in this thread) contains a script, so it shouldn’t be hard to guess “Which one, please?”.:wink:

Its here again. http://www.sitepoint.com/forums/showpost.php?p=1084630&postcount=6.

This is the JavaScript Forum. This thread is in the JavaScript Forum. I advice you not to post multiple threads for a same problem. Its not looked upon nicely.
I’ve posted the link to the solution of your problem. I think that you shouldn’t have any trouble clicking it to read further.:wink: :smiley: