Javascript firefox extension to get text by link

Is it possible for me to expect the user to click the link and after clicking the link the link text will be received?

Perhaps using onClick?

+1


a source to share


2 answers


If you mean handling click events for links on the page the user is viewing, here's how to do it:



// handle the load event of the window  
window.addEventListener("load",Listen,false);  
function Listen()  
{   
gBrowser.addEventListener("DOMContentLoaded",DocumentLoaded,true);  
}  

// handle the document load event, this is fired whenever a document is loaded in the browser. Then attach an event listener for the click event  
function DocumentLoaded(event) {  
 var doc = event.originalTarget;
 doc.addEventListener("click",GetLinkText,true);  
}  
// handle the click event of the document and check if the clicked element is an anchor element.  
function GetLinkText(event) {  
   if (event.target instanceof HTMLAnchorElement) {  
    alert(event.target.innerHTML);
   }   
} 

      

+6


a source


It's very easy using jQuery:

<script>
    $(document).ready(function(){
        $("a").click(function(){alert($(this).text());});
    });
</script>

      



Of course, you probably want to do something other than a text warning.

+1


a source







All Articles