Created
July 6, 2014 08:02
-
-
Save jonathantneal/a82600f12804589c0f0b to your computer and use it in GitHub Desktop.
IndieAuth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (NAME, SEPARATOR) { | |
var | |
ATTR = 'data-' + NAME, | |
SELECTOR = '[' + ATTR + ']'; | |
function onupdate() { | |
// for all matching elements | |
Array.prototype.forEach.call(document.querySelectorAll(SELECTOR), function (element) { | |
var | |
// get data from attribute | |
data = element.getAttribute(ATTR), | |
// detect separator | |
separatorIndex = data.indexOf(SEPARATOR) !== -1 ? data.indexOf(SEPARATOR) : data.length, | |
// separate feature from options | |
feature = data.slice(0, separatorIndex), | |
options = data.slice(separatorIndex + 1); | |
// remove attribute | |
element.removeAttribute(ATTR); | |
// do something with the feature and options | |
console.log(feature, options); | |
}); | |
} | |
// update now or when DOM is ready | |
if (/e/.test(document.readyState)) onupdate(); | |
else document.addEventListener('DOMContentLoaded', onupdate); | |
// update when DOM changes | |
document.addEventListener('DOMSubtreeModified', onupdate); | |
})('indieauth', ':'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment