Given the following HTML:
<html>
<head>
<link href="http://example.com/g/8c" rel="shortlink" />
</head>
<body></body>
</html>
How would I retrieve the contents of href=”” in the link, rel=shortlink with javascript? I don’t know if such items in the head are accessible in the DOM.
The head-tag may contain more link-tags, for assets such as css. The head-tag may contain more then one link-shortlink (as per HTML5 ‘specs’), in that case, just pick the first.
You can use
document.headto get the head, and then usegetElementsByTagNameto get thelinktags.NOTE:
document.headmight not work in all browsers (by that, I mean it might not work in IE). If it’s not working try this: