Created
December 2, 2013 23:08
-
-
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
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
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