Forum Moderators: open

Message Too Old, No Replies

JS Conditional Link

Only changing page if user OKs alert box

         

mcfly

2:43 pm on Nov 1, 2004 (gmt 0)

10+ Year Member



Hi all,

I'm producing a web interface to administrate a database, and have a series of links to edit/delete records.

Is there a way (with JS) to only navigate through a link if a user confirms their intention with an alert box, or some other condition for that matter?

Alternatively, can a change of page be initiated purely by a JS command?

Thanks for your help.

Cook

2:57 pm on Nov 1, 2004 (gmt 0)

10+ Year Member



You can do it this way:

<a href="myurl.htm" onclick="return mycondition()">my link</a>

If the event onclick returns false, the link is not followed. So just have mycindition() above return false, and the link will not be followed.

mcfly

3:00 pm on Nov 1, 2004 (gmt 0)

10+ Year Member



Thanks Cook, I thought I'd tried that and it hadn't worked, but I'll have another go. Cheers.

StupidScript

5:39 pm on Nov 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<a href="javascript:void(mycondition('myurl.htm'))">my link</a>

Refers to a function that pops up your confirmation window:

<script ... >
function mycondition(loca) {
// pops up a "prompt" window
activateLink=prompt("Do you really want to go?");
if (activateLink) {
// they clicked "OK", so off they go
top.location.href=loca;
}
}
</script>

And yes, you can redirect a visitor based exclusively on a Javascript instruction, but I'm not sure what use you have for it.

mcfly

9:57 am on Nov 2, 2004 (gmt 0)

10+ Year Member



Perfect, thanks StupidScript.

It was the top.location.href=loca; line that I wasn't familiar with and was causing a lot of trouble!

Cheers!