tags:

views:

235

answers:

4

I am trying to send the user to either google or yahoo depending on which link they clicked, if they clicked the 1st one they will be taken to google, and if they clicked the seond they will be taken to yahoo.

So for the window.location function I am using a variable called website which I'm defining based on which link the user clicked. But it's not working. The variable isn't being recognized:

<script type="text/javascript">
    $(document).ready(function(){

        // define the variable website based on which link is clicked
        $('a').eq(0).click(function() {
            var website = 'http://google.com';
        });
        $('a').eq(1).click(function() {
            var website = 'http://yahoo.com';
        });

        $('a').click(function() {
            window.location = website;
        });

    });
</script>

<a href="#">Link 1</a>
<br />
<a href="#">Link 2</a>
A: 
<script type="text/javascript">

    var _website = null;

    $(document).ready(function ()
    {
        $('a').eq(0).click(function ()
        {
            _website = 'http://google.com';
            Navigate();
        });

        $('a').eq(1).click(function ()
        {
            _website = 'http://yahoo.com';
            Navigate();
        });
    });

    function Navigate()
    {
        document.location = _website;
    }

</script>

<a id="link1" href="#">Link 1</a>
<br />
<a id="link2" href="#">Link 2</a>
Tim
After declaring it outside the click delegates it is now being recorgnozed but script stops executing after detecting the first click. In other words it doesn't go down to the window.location function. Just stops after one of the above link click functions. So the variable issue is solved, but how do I let the script go keep executing all the way through the window.location function. Kind of hard to explain but you will see what I mean if you run it.
Dan
@Dan - yes, I am running it now to give you a complete answer.
Tim
@Tim I should have explained, the 2 links and going to yahoo or google isn't really what I am trying to achive, I just used that example to illustrate what I needed, which is to use one function with a variable inside, and that variable would be defined prior based on which indexed link is clicked. I need that particular method to work specifically. I know, the above example can be achieved in a much easier way like the way you posted, but I need that index/variable/variable defined by which link is clicked way to work.
Dan
@Dan - see my latest example. The code works correctly; see if it meets your business case.
Tim
@Tim yes that works perfectly, thanks so much for taking the time to help!
Dan
+1  A: 

More better way of doing it -

<script type="text/javascript">
  $(document).ready(function(){
   var website = '';
   $('a').click(function() {
    var clicked = $(this).index('a');
    switch(clicked){
    case 0:
    website = "http://google.com";
    break; 

    case 1:
    website = "http://rediff.com";
    break;        
    }
     document.location= website;

     });
   });
</script>

<a href="#">Link 1</a>
<br />
<a href="#">Link 2</a>
Alpesh
according to this question I just asked, eq can be used in the way you mentioned and the way I was using, both work: http://stackoverflow.com/questions/3848238/how-to-detect-the-2nd-link-on-a-page-using-jquery
Dan
i have written my code in a more efficient way just give it a try.
Alpesh
A: 

Just out of curiosity I am asking, why do we need jquery or javascript for these clicks, is it for learning jquery or in real time.

gov
A: 
<a id="link1" href="http://google.com"&gt;Link 1</a> 
<br /> 
<a id="link2" href="http://yahoo.com"&gt;Link 2</a>
Ricardo