<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)?
<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>
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
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.
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?”.
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.