Undefined href! (DOM API)
On google's search results page,
document.getElementsByClassName("l")[0]
//"l" is the class for the URL's of the results.
Returns undefined.
However,
alert(document.getElementsByClassName("l")[0]);
Will display the actual URL of the first result.
I just need to access the URL's of the results!
//Adding
.href //to the end does not help
You 开发者_JS百科can experience this by going to your browser console and entering
document.getElementsByClassName("l")[0]
If you want just the href
attribute:
document.getElementsByClassName('l')[0].getAttribute('href');
This works fine in Firefox/Firebug, Safari, and Chrome.
EDIT: it works fine in the search results, I was doing it in the homepage initially.
document.getElementsByClassName("l")
精彩评论