Skip to content

Instantly share code, notes, and snippets.

@nuxodin
Last active June 9, 2020 05:48
Show Gist options
  • Save nuxodin/aa4370c5472786fe090f188cbe2661d4 to your computer and use it in GitHub Desktop.
Save nuxodin/aa4370c5472786fe090f188cbe2661d4 to your computer and use it in GitHub Desktop.
Enable each element to be linked
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
/**
ussage:
<table>
<tr data-c1-href="https://example.com" data-c1-target="_blank">
<td> <a href="https://example.com" target="_blank">example.com</a>
<td> Do not forget to add a real link so that your content is accessible.
</table>
*/
c1 = {
href: {
ignoreSelector:'[onmousedown]'
}
};
document.addEventListener('click',function(e){
if (e.which !== 1) return;
if (e.defaultPrevented) return;
if (!e.target.closest) return;
var A = e.target.closest('[data-c1-href]');
if (!A) return;
if (e.target.closest('a,input,textarea,select,button')) return;
if (e.target.closest('[fn], [onclick]')) return;
if (e.target.closest(c1.href.ignoreSelector)) return;
if (e.target.isContentEditable) return;
var href = A.getAttribute('data-c1-href');
if (!href) return;
var target = A.getAttribute('data-c1-target');
if (e.ctrlKey) target = '_blank'; // better random-string?
if (target) {
window.open(href, target);
//!e.ctrlKey && win.focus(); // not needed in chrome, not working in ff
} else {
location.href = href;
}
});
document.head.insertAdjacentHTML('beforeend','<style>[data-c1-href] {cursor:pointer},[data-c1-href=""]{cursor:normal}</style>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment