confirm() function doesn't seem to work

E

Eric

I have a confirm alert before the user goes to a delete page, and it
doesn't matter if the user clicks ok or cancel - they always go to the
delete page.

I have the following code:

<a href="vehicle_history_delete.php?history_id=18&vehicle_id=5"
onclick="return DelAlert();"><img border=0
src="/images/delete_category_16.gif" alt="Delete This record"
title="Delete this record">

Where DelAlert() looks like:

<script language="JavaScript" >

function DelAlert() {
if (confirm("Are you sure you want to delete this history record?"))
{
return true;
} else
{
alert('Should be false');
return false;
}
}

</script>

And have tried

<a href="vehicle_history_delete.php?history_id=10&vehicle_id=5"
onclick="return confirm('Are you sure you want to delete?')">
<img border=0 src="/images/delete_category_16.gif" alt="Delete This
record" title="Delete this record">
</a>

as well.

This DOES prevent the navigation to the delete page:

<a href="vehicle_history_delete.php?history_id=10&vehicle_id=5"
onclick="return false;">

But is obviously not acceptable.

Here's the kicker. The same code on a different page works fine. This
page does not work in either IE or Firefox.

Ever happen to anybody before?

Eric
 
L

Lee

Eric said:
I have a confirm alert before the user goes to a delete page, and it
doesn't matter if the user clicks ok or cancel - they always go to the
delete page.

I have the following code:

<a href="vehicle_history_delete.php?history_id=18&vehicle_id=5"
onclick="return DelAlert();"><img border=0
src="/images/delete_category_16.gif" alt="Delete This record"
title="Delete this record">

Where DelAlert() looks like:

<script language="JavaScript" >

function DelAlert() {
if (confirm("Are you sure you want to delete this history record?"))
{
return true;
} else
{
alert('Should be false');
return false;
}
}

</script>

And have tried

<a href="vehicle_history_delete.php?history_id=10&vehicle_id=5"
onclick="return confirm('Are you sure you want to delete?')">
<img border=0 src="/images/delete_category_16.gif" alt="Delete This
record" title="Delete this record">
</a>

as well.

This DOES prevent the navigation to the delete page:

<a href="vehicle_history_delete.php?history_id=10&vehicle_id=5"
onclick="return false;">

But is obviously not acceptable.

Here's the kicker. The same code on a different page works fine. This
page does not work in either IE or Firefox.

The code you posted works fine.
There's something else in your page that's clobbering it.
Have you defined a "confirm()" function that's replacing the built-in?


--
 
E

Eric

The code you posted works fine.
There's something else in your page that's clobbering it.
Have you defined a "confirm()" function that's replacing the built-in?
Nope. It was a good thought, so I checked, but if I did I wouldn't
even see the message - which I do.

E
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 29 Sep 2006 12:34:31 remote, seen in
news:comp.lang.javascript said:
function DelAlert() {
if (confirm("Are you sure you want to delete this history record?"))
{
return true;
} else
{
alert('Should be false');
return false;
}
}

(1) There is no need for an else part if the previous code returns.
(2) The following is simpler :-

function DelAlert() { var OK
OK = confirm("Are you sure you want to delete this history record?")
if (!OK) alert('Should be false')
return OK }

It's a good idea to read the newsgroup and its FAQ. See below.
 
E

Evertjan.

Dr John Stockton wrote on 30 Sep 2006 in comp.lang.javascript:
function DelAlert() { var OK
OK = confirm("Are you sure you want to delete this history record?")
if (!OK) alert('Should be false')
return OK }

function DelAlert() {
if (confirm("Are you sure you want to delete this history record?")){
alert('Should be false');
return false;
};
// return true; // unneccessary?
}
 
T

Touffy

function DelAlert() {
if (confirm("Are you sure you want to delete this history record?")){
alert('Should be false');
return false;
};
// return true; // unneccessary?
}

That would return false when the dialog is confirmed.

Anyway the alert() line is obviously a temporary debug line. The
function could be further simplified as :

function DelAlert() {
return confirm("Are you sure you want to delete this history record?")
}

at which point it may be just as well to inline it.

I'm not sure why it doesn't work, but you could work around the problem
by using the function to change the document.URL instead of cancelling
the navigation, and instead of a link, use a button, or an input
type="image". Links aren't supposed to perform actions like deleting
something anyway.
 
E

Evertjan.

Touffy wrote on 01 Oct 2006 in comp.lang.javascript:
That would return false when the dialog is confirmed.

Anyway the alert() line is obviously a temporary debug line. The
function could be further simplified as :

function DelAlert() {
return confirm("Are you sure you want to delete this history
record?")
}

at which point it may be just as well to inline it.

I'm not sure why it doesn't work,

What does not work?
 
E

Eric

Evertjan. said:
Touffy wrote on 01 Oct 2006 in comp.lang.javascript:


What does not work?

The confirm(). It doesn't prevent navigation to the delete page. It
doesn't work inline or if I put it in another function. That is why I
posted the original question.

I will take David Junger's (good) advice though and change the pages to
not use a hyperlink for the delete. (He was right about the alert being
a temporary debugging thing too) I will use an input type=image and
see how that works. (A button won't look right, which is why I used a
hyperlink in the first place)

Eric
 
E

Evertjan.

Eric wrote on 01 Oct 2006 in comp.lang.javascript:
Evertjan. wrote: [..]
What does not work?

The confirm(). It doesn't prevent navigation to the delete page. It
doesn't work inline or if I put it in another function. That is why I
posted the original question.

Well let's go back to basics:

============= test.html ===============
<a
href="vehicle_history_delete.php?history_id=18&vehicle_id=5"
onclick="return DelAlert();">
click me [instead of the img]</a>

<script type='text/javascript'>
function DelAlert() {
return confirm("Are you sure you want to delete?")
}
</script>
========================================

The above .html works fine, just try it,
so you must have [made?] an error elsewhere in your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top