JavaScript OnClick

Discussion in 'JavaScript' started by kiransarv, Nov 15, 2008.

  1. #1
    Hi all,

    My browser is firefox.

    I have the piece of code

    <html>
    <head>
    <script type="text/Javascript">

    function load(url,event)
    {
    location.href=url;
    }

    </script>
    </head>
    <body>
    <a href="http://www.yahoo.com" onClick="load('http://www.google.com'); return false;">Yahoo</a>
    </body>
    </html>

    The code works fine when i press left button. But it will not redirect me to google when i press both middle and right(open link in new tab).

    Then i tried this.

    <html>
    <head>
    <script type="text/Javascript">

    function load(url,event)
    {
    var button = event.which;
    //document.write(button);

    if(button==1)
    {
    window.location = url;
    }

    if(button==2)
    {
    window.open(url);
    }

    }

    </script>
    </head>
    <body>
    <a href="http://www.yahoo.com" onMouseDown="load('http://www.google.com',event); return false;">Yahoo</a>

    </body>
    </html>

    It is displaying button values but unable to redirect. So How can i redirect to google when i click yahoo link in all the three cases.

    regards
    kiran
     
    kiransarv, Nov 15, 2008 IP
  2. ariefsyu

    ariefsyu Active Member

    Messages:
    192
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    <html>
    <head>
    <script type="text/Javascript">
    
    function load(url)
    {
    window.open(url,'_blank','scrollbar=yes') ;
    }
    
    </script>
    </head>
    <body>
    <a href=#  onClick="load('http://www.google.com'); return false;">Yahoo</a>
    </body>
    </html>
    Code (markup):
    This js code open in a new window. I have try some way to open in a new tab, but it doesn't works.

    Sorry.
    Arief
     
    ariefsyu, Nov 16, 2008 IP
  3. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Hey, remember to use onclick="" instead of onClick="", you can't capitalize "click" in the new w3 standards :)
     
    elias_sorensen, Nov 17, 2008 IP