Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to extract target of an anchor under mouse pointer

    I am trying to redevelop firefox addon, to give it more funcionality. I found JS file where all the functions are and started to edit it.

    What I want to achieve is to get target of an anchor under mouse pointer (when mouse pointer is over anchor, I right click and call addon from context menu).

    For example when I have anchor which HTML code is:

    Code:
    <a href="somewehere.com/place">place</a>
    when I right click on this code and call my addon I would like to alert its href (somewehere.com/place)

    I wrote a function:

    Code:
    function ff()
    {
    var current_target=this.href;
    alert(current_target);
    }
    but it gives me udefined on alert



    Any hints to achieve this are highly appreciated.

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <a href="http://www.google.com" onclick = "ff(this)">place</a>
    
    <script type = "text/javascript">
    function ff(which) {
    var current_target=which.href;
    alert(current_target);
    }
    </script>
    Or more simply:-


    Code:
    <a href="http://www.google.com" onclick = "ff(this.href)">place</a>
    
    <script type = "text/javascript">
    function ff(which) {
    alert (which);
    }
    </script>
    Quizmaster: The TV show "The Merits of Ferrets" stars which kind of animal?
    Contestant: I don't know.
    Quizmaster: There's a clue, if you know where to look.
    Contestant: I'm sorry, no.
    Quizmaster (laughing): The answer is ferrets.
    Contestant: Well, how was I supposed to know that?
    Last edited by Philip M; 09-30-2011 at 10:07 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Philip M, thanks for your attention

    But in my case link does not have onclick event, which is used by left click.

    In my case it would be:

    Code:
    <a href="http://www.google.com">place</a>
    
    <script type = "text/javascript">
    function ff() {
    var current_target=href;
    alert(current_target);
    }
    </script>
    But it doesn't work.

    To clarify, let's imagine you right click on a link, context menu pops out, and you choose position "Copy shortcut" - I need THIS shortcut.

    Thanks anyway

    ------------------------------------

    I also tried:

    Code:
    function ff()
    {
    var rr=document.links[this].href;
    alert(rr);
    }
    But it doesn't work, but it shows what I want to achieve. I want to get href of THIS (mouse is over it) link...
    Last edited by pc131; 09-30-2011 at 10:27 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •