JS Anchor tag HREF attribtue query

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 google.com 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 google.com is done with its execution.
If that is the case how can I know the exact URL?