aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes JS Anchor tag HREF attribtue query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JS Anchor tag HREF attribtue query" Watch "JS Anchor tag HREF attribtue query" New topic

JS Anchor tag HREF attribtue query

Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

When we do a google search, the results are usually rendered as anchor elements. When we hover over them, we would be able to see the target web page the user will be redirected to if he clicks it. Now if im not wrong.. this should be because of HREF attribute of the ANCHOR HTML element.
So far so good. Coming to the point, i am writing a java code which would register the following java script code to all the anchor tags that are displayed in the browser. So, ideally if I hit a search result, I would get an ALERT dialog providing me the HREF attribute.

String line1 = "var elements = document.getElementsByTagName('a'); \r\n";
String line2 = "for(var i=0" + ";" + "i<elements.length" + ";" + "i++){ \r\n";
String line4 = "elements[i].onmousedown = function(){ \r\n";
String line5 = "alert(this.getAttribute('href'));\n" + "}" + "} \r\n";
But surprisingly, if I open and if I hit say, IMAGES/YOUTUBE/GMAIL/DRIVE, I am getting the alert box. However, if I hit any of the search resutls i am not getting the alert box.
I am not a js guru. So, I have no idea of what's going on. Per inputs from other guys I could understand that the href attribute is something that shall be constructed after the js on is done with its execution.
If that is the case how can I know the exact URL?

Requesting you to share your ideas on this.

Have you tried LearnNowOnline?
subject: JS Anchor tag HREF attribtue query