I need help with a little script and don't know enough about javascript to accomplish it myself. I have this working but only when linking to #. ex. - (a href="#" onclick="goout();") here is the function var play; function goout(move) { var go = "my base url" + move var userInput = confirm('Are you sure you want to go there?'); if (userInput) { window.location = go; } else { window.location = 'my other page'; }; } Code (markup): Like I said, this code works fine when I link to # but I don't want to link to #, I want to link to the equivalent of the variable go, if that makes any sense.
But how would you know ahead of time what is in the variable go, or are you hoping to make it so it already has all the out links you want as the hrefs and you want to make it so they confirm they want to leave the page or not? I'd say the best method would be to return false if they don't want to leave and return true if they do, and if an onclick returns false then it won't make the href go. <a href="your link here" onclick="return goout();"> function goout(move) { var userInput = confirm('Are you sure you want to go there?'); if (userInput) { return true; } else { return false; }; } Code (markup): You could probably even trim that function down and just do function goout() { return confirm('Are you sure you want to go there?'); }
I think this is what you want <SCRIPT> function goout(move) { var userInput = confirm('Are you sure you want to go there?'); if (userInput) { window.location = move; return true; } else { return false; }; } </SCRIPT> <A HREF=http://google.com onClick="return goout('http://google.com');">Click</A>
Your method doesn't even need the return though since you're also doing the window.location before you return. Yours is basically my method + his method combined, it'd be easier just to do the return and not do window.location at all. Also I'm not sure if it's good to double post :<
I'll take all the double posts I can get if it fixes my issue. Thanks for all of your help, this is what worked for me. function goout(move, out) { var go = "outgoing url" + move var userInput = confirm('Are you sure you want to go there?'); if (userInput) { window.location = go; return true; } else { window.location = out; return false; }; } Code (markup): Not sure if I need the return true statement or not but I do know that it works perfectly.