polyfill for null anchor hrefs

by atdt

HTML

<dl>
    <dt> Generic link
    <dd> <a href="http://www.w3.org/">W3C</a>

    <dt> Eponym (1st variant)
    <dd> <a href>http://www.gnu.org/</a>
    
    <dt> Eponym (2nd variant)
    <dd> <a>http://www.gnu.org/</a>
</dl>

CSS

dt:after { content:":"; }
dt       { clear: left; float: left; }
body     { font-family: sans-serif; }
dt, dd   { padding: 0.4em; }

JavaScript

// Polyfill for eponymous <a> elements
var anchors = document.getElementsByTagName('a');

Array.prototype.forEach.call(anchors, function(a) {
    // First variant: <a href>http://w3.org/</a>
    if (a.getAttribute('href') === '') {
        a.setAttribute('href', a.textContent);
    }
    // Second variant: <a>http://w3.org/</a>
    if (a.getAttribute('href') === null) {
        a.setAttribute('href', a.textContent);
    }
});