Anchor Tags and OnClick

Discussion in 'JavaScript' started by hammurabbi, Dec 27, 2006.

  1. #1
    This is either perfectly simple or quite impossible, but no amount of searching has found me the answer.

    For reasons too complex to get into, I want to run a JavaScript function when a user clicks on a hyperlink. But I need the link to work as normal too.

    <a href="http://example.com/" onclick="javascript:myFunction();">
    Code (markup):
    In the above example, I want myFunction to execute AND I want the user to wind up at example.com.

    Is it possible? How would I do it?

    Currently, it doesn't execute the funtion. If I add a "return false" to the onclick, it executes the function but doesn't open the link.

    (if you need to know what I'm trying to achieve -- the URL in my real code references an external application in much the same way as a mailto: would open Outlook. When the user clicks the link, I want the link to open the external application, then run the myFunction (which dynamically rewrites that URL for when it is clicked next))

    Any help would be greatly appreciated. I'm quite new to JavaScript.
     
    hammurabbi, Dec 27, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Could you post the code of myFunction() ? And also, there's no need for the "javascript:" in the onclick attribute as the browser expects a script there.
     
    nico_swd, Dec 27, 2006 IP
  3. hammurabbi

    hammurabbi Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Nico_SWD. I've removed the "javascript:" bit, and will remember that in future.

    I'm not sure what the point of posting the code for myFunction would be. It works fine when it executes. My problem is that it doesn't execute.
     
    hammurabbi, Dec 27, 2006 IP