Skip to content

Instantly share code, notes, and snippets.

@kgaughan
Created January 22, 2019 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgaughan/4c744b794a23e2f022b08c82a29ca84d to your computer and use it in GitHub Desktop.
Save kgaughan/4c744b794a23e2f022b08c82a29ca84d to your computer and use it in GitHub Desktop.
Some old JS code from an older version of my site. The timestamp is April 2006, but it probably dates from earlier. Looks to be a way to make smart blockquotes.
function addEvent(obj, type, fn) {
// Mozilla/W3C listeners?
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
return true;
}
// IE-style listeners?
if (obj.attachEvent) {
return obj.attachEvent('on' + type, fn);
}
return false;
}
addEvent(window, 'load', function() {
if (!document.createElement) {
return;
}
// For each block quote...
var quotes = document.getElementsByTagName('blockquote');
for (var i = 0; i < quotes.length; ++i) {
// ...that includes a citation...
var cite = quotes[i].getAttribute('cite');
if (cite != '' && cite != null) {
// Create a link to the cited resource...
var a = document.createElement('a');
a.setAttribute('href', cite);
var titleNode;
var title = quotes[i].getAttribute('title');
if (title == '' || title == null) {
a.setAttribute('title', cite);
titleNode = document.createTextNode('Source');
} else {
a.setAttribute('title', title);
titleNode = document.createTextNode(title);
}
a.appendChild(titleNode);
// ...wrap it in a paragraph with class 'source',...
var p = document.createElement('p');
p.className = 'source';
p.appendChild(a);
// ...and put it at the end of the blockquote.
quotes[i].appendChild(p);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment