Links calling a function

Discussion in 'HTML & Website Design' started by PrateekSaxena, Oct 23, 2006.

  1. #1
    Hello,

    I want to have links on my page that actually call a JavaScript function. I can do this by writing-

    <A onClick="foo()">Click Me!</A>
    HTML:
    But when I write this what happens is that the 'a:hover' code that I had written in the CSS file does not have anny effect on the link. If I write something inside the HREF="" then only does it have any effect. But I cannot call the JavaScript function by writing it inside HREF. What do I do so that I can call a function of JavaScript without losing the effect that the CSS should have.

    Thanking You,
    Prateek Saxena :)
     
    PrateekSaxena, Oct 23, 2006 IP
  2. missdanni

    missdanni Guest

    Best Answers:
    0
    #2
    Have you tried:

    <a href="#" onClick="foo()">Link</a>

    - Danni

     
    missdanni, Oct 23, 2006 IP
  3. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In addition to what missdanni said, you can have the function return false so JS-enabled browsers will not risk having the page jump.
     
    Gordaen, Oct 23, 2006 IP
  4. PrateekSaxena

    PrateekSaxena Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank You so much missdanni!! I have been trying to achieve this for months.Thanks
     
    PrateekSaxena, Oct 23, 2006 IP
  5. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The "return false;" is great for accessibility, if you want to open popup but if browser dont support js so it will open in the same window :)

    ex.

    <a href="bla.html" onclick="popup(this.href);return false;">Bla bla bla</a>
    HTML:
     
    Morishani, Oct 23, 2006 IP
  6. missdanni

    missdanni Guest

    Best Answers:
    0
    #6
    You are most welcome :)
     
    missdanni, Oct 24, 2006 IP