Skip to content

Instantly share code, notes, and snippets.

@octavioamu
Created December 2, 2013 23:08
Show Gist options
  • Save octavioamu/7760748 to your computer and use it in GitHub Desktop.
Save octavioamu/7760748 to your computer and use it in GitHub Desktop.
Let you define an href on any element: https://github.com/nathanford/data-href
sethrefs = function () {
if (document.querySelectorAll) {
var datahrefs = document.querySelectorAll('[data-href]'),
dhcount = datahrefs.length;
while (dhcount-- > 0) {
var ele = datahrefs[dhcount],
link = function (event) {
var target = event.target,
leftclicked = ('buttons' in event && event.buttons === 1) ? true :
('which' in event && event.which === 1) ? true :
(event.button === 1) ? true : false;
if (!event.target.href) window.location.href = this.getAttribute('data-href');
};
if(ele.addEventListener) ele.addEventListener('click', link, false);
else ele.attachEvent('onclick', link);
}
}
};
sethrefs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment