javascript redirect

Discussion in 'JavaScript' started by mikebrad0927, Apr 27, 2008.

  1. #1
    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.
     
    mikebrad0927, Apr 27, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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?');
    }
     
    zerxer, May 1, 2008 IP
  3. joshhrob

    joshhrob Active Member

    Messages:
    97
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    98
    #3
    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>
     
    joshhrob, May 1, 2008 IP
  4. joshhrob

    joshhrob Active Member

    Messages:
    97
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    98
    #4
    By putting return in the onclick it will only redirect you if you click OK
     
    joshhrob, May 1, 2008 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 :<
     
    zerxer, May 1, 2008 IP
  6. mikebrad0927

    mikebrad0927 Peon

    Messages:
    266
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    mikebrad0927, May 2, 2008 IP