Jump to content

Confirm Alert Troubles


Err

Recommended Posts

Hello, I'm having trouble with a javascript confirm alert. What I'm trying to do is have the visitor click on a link with an onClick added to it, which in then brings up the confirm alert to either stay or go to the link, my only problem is the only scripts out there that I found only look like this:

<script type="text/javascript">function ConfirmChoice(){answer = confirm("Do you really want to go here?")if (answer !="0")  {location = "index.html"  }}</script>

The above scripts require me to set a location to the targeted webpage for EACH function (if I wanted more then one function for each different link that is), I don't really want to use this script that way since it's too time consuming when adding alerts to new links.

<a href="index.html" onclick="confirm("Press a button")">Text</a>

The above way almost works, but the "Cancel" option doesn't really cancel the operation.My problem is that I need a script that just gives the visitor the same alert but with out having to go to the same 'location' with each onclick or (in the other case) with an alert that acutally works correctly. I don't want to add anymore functions then what's nessary to repeat the same script. ANY help is greatly appericated!

Link to comment
Share on other sites

The easiest way of doing this is just to insert "return" into your call to confirm():

<a href="http://www.w3schools.com" onclick="return confirm('Do you really want to go here?');">LINK</a>

If the message "Do you really want to go here?" is going to be the same for every link, then it will be slightly preferable to use your own function that you can call without any parameters and which will call the confirm. Doing it this way avoids duplication of the text and leaves you with only one place to change it if you decide to reword it:

function confirmLink(){  return (confirm("Do you really want to go here?"))}

and the link:

<a href="http://www.w3schools.com" onclick="return confirmLink();">LINK</a>

Link to comment
Share on other sites

THANK YOU SO MUCH!!! *Bows down* That's exactly what I needed.You wouldn't believe how long I tackled this problem, and you got it within mintues of my post! This is so awsome man, you're aswome! Next time I know who to talk to when I need some javascript help :) Thanks a lot again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...