/    Sign up×
Community /Pin to ProfileBookmark

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>[/CODE]

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);
}
[/CODE]

but it gives me udefined on alert

[URL=”http://i56.tinypic.com/au7gk1.png”]UNDEFINED[/URL]

Any hints to achieve this are highly appreciated.

Thanks

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JunkMaleOct 01.2011 — try adding the onmouseover="this.title=this.href;" tag to the anchor so it should then on any anchor you hover a mouse over, it will display the name of the href or url.

if you need it in a function...

[CODE]function whereAmI(o){

o.title = o.href;

}[/CODE]


then onmouseover="this.title=this.href;" becomes onmouseover="whereAmI(this);"

if you want an alert, you create a function lie ff()

[CODE]function myalert(o){
alert( o.href );
}[/CODE]


and using one of the onclick events like onmouseover, you call the function in the same way as you set up the onmouseover, pass the 'this' reference as a parameter to the myalert function like this, myalert(this)

Does that help?
Copy linkTweet thisAlerts:
@pc131authorOct 21.2011 — Hi JunkMale

Thank You for your input. Sorry for such late response, haven't noticed this...

I am aware of this javascript thing, anyway what I need is to get the URL of hyperlink when I RIGHT-CLICK not left-click or point mouse over it.

Currently I am trying to extend firefox extension, which works in following way:

When I mark text on page which is in fact hyperlink (for example I mark http://server1.com/file2.zip) and RIGHT-CLICK and choose option GRAB-THIS-FILE, the file2.zip is the downloaded by this extension.

I digged deep into the code and what I need is to read the URL of underlying hyperlink, so when I RIGHT-CLICK on hyperlink (for example GRAB_THE_FILE) and choose GRAB-FILE-OF-THIS-HYPERLINK, the file described in HREF should be downloaded by this extension.

I tried the way that you specified but it doesn't work.

Thanks anyway, I may abandon this as this is more of my free time doing than duties,

Tom
Copy linkTweet thisAlerts:
@xelawhoOct 22.2011 — why not just:

[CODE]
<a href="somewehere.com/place" oncontextmenu="doSomething(this.href)">place</a>

<script type="text/javascript">
function doSomething(a) {
alert(a);
}
</script>[/CODE]
Copy linkTweet thisAlerts:
@JunkMaleOct 22.2011 — I tried the way that you specified but it doesn't work.[/QUOTE]
I fail to see how it wouldn't work. its likely if this is an extention of firefox, its a method thats not supported.

The onmouseover="functioncall(this)" when called passes the object this which points to the elements parent that called it.

So
[CODE]
<a href="somewehere.com/one" onmouseover="whereAmI(this)">One Link</a>
<a href="somewehere.com/two" onmouseover="whereAmI(this)">Two Link</a>
<a href="somewehere.com/three" onmouseover="whereAmI(this)">Three Link</a>
<script>
function whereAmI(o){

o.title = o.href;

}
</script>[/CODE]


each call to the whereAmI() function which is passed the property of this will show a different URL.

I tried this on my computer as shown by the screen shot.
Copy linkTweet thisAlerts:
@Logic_AliOct 22.2011 — 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).
[/quote]
This should work on browsers that allow scripts to read right clicks. Just pass it your own function:&lt;script type='text/javascript'&gt;

function getRightClickedLink( userFunc ) /* Calls userFunc( link reference ) */
{
function installHandler( obj, evt, func )
{
window.attachEvent ? obj.attachEvent( evt, func ) : obj.addEventListener( evt.replace(/^on/i, ""), func, false );

<i> </i>return func;
}

function f( e /*2843294C6F67696320416C69*/ )
{
var evt = e || window.event,
srcElem = evt.target || evt.srcElement,
clickedElem = srcElem;

<i> </i>if( evt.button &gt; 1 )
<i> </i>{
<i> </i> while( srcElem &amp;&amp; srcElem.nodeName !== 'A' )
<i> </i> srcElem = srcElem.parentNode;

<i> </i> if( srcElem )
<i> </i> userFunc( srcElem );
<i> </i>}
}

installHandler( document, 'onmousedown', f ); <br/>
}

getRightClickedLink( function( lnk ){ alert( 'The right-clicked link's href is: ' + lnk.href ); } );

&lt;/script&gt;
Copy linkTweet thisAlerts:
@xelawhoOct 22.2011 — just out of curiosity, can someone explain why oncontextmenu is no good here? I've tested it in IE, FF and Chrome and it seems to work fine ?

if you don't want to add that snippet to every link in your html you can do it by looping through them in the js:

[CODE]<html>
<head>

</head>

<body onload="setListeners ()">
<a href="somewehere.com/place">place</a><br>
<a href="somewehere.com/time">time</a><br>
<a href="somewehere.com/date">date</a><br>

<script type="text/javascript">

function setListeners () {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var link = links[i];
link.oncontextmenu=function(){doSomething(this.href)};
}
}

function doSomething(a) {
alert(a);
}
</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@Logic_AliOct 23.2011 — just out of curiosity, can someone explain why oncontextmenu is no good here? I've tested it in IE, FF and Chrome and it seems to work fine

if you don't want to add that snippet to every link in your html you can do it by looping through them in the js:
[/quote]


You could listen to oncontextmenu but I used mousedown to show the alert before the context menu can appear. In fact it prevents it appearing without disabling the event, which FireFox can be configured to prevent.

The code I gave doesn't have to be assigned to links, because it listens to the bubbled event on [FONT=Courier New]document[/FONT] and checks its origin.
×

Success!

Help @pc131 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...