Skip to content

Instantly share code, notes, and snippets.

@nuc
Created September 8, 2016 11:14
Show Gist options
  • Save nuc/2b40b48b193db3c5c6d95d067d37743c to your computer and use it in GitHub Desktop.
Save nuc/2b40b48b193db3c5c6d95d067d37743c to your computer and use it in GitHub Desktop.
Debug tabindex with javascript by listening "tab" key event and show in console the active element. Tested in Chrome 25 and Firefox 19
(function(){
"use strict";
document.addEventListener('keydown', function (event) {
setTimeout(function () {
var key, win, frameEl, activeEl, uri, msg;
msg = "";
key = window.event ? event.keyCode : event.which;
//Tab
if (key === 9) {
activeEl = document.activeElement;
while (activeEl && activeEl.tagName === 'IFRAME') {
activeEl = activeEl.contentWindow.document.activeElement;
}
if (activeEl) {
msg += activeEl.id + ' - ' + activeEl.tagName;
win = activeEl.ownerDocument.defaultView;
uri = win.location.href;
msg += ' - ' + uri;
msg += ' (tabindex=' + activeEl.getAttribute('tabindex') + ')';
}
frameEl = win.frameElement;
if (frameEl) {
msg += 'in frame ' + frameEl.id + ' (tabindex=' + frameEl.getAttribute('tabindex') + ')';
}
console.log(msg);
console.log(activeEl, frameEl);
activeEl = null;
}
}, 500);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment