Last active
March 11, 2020 20:11
-
-
Save monochromer/226d5559d582f422ed4aedcb9d482286 to your computer and use it in GitHub Desktop.
Fix skip link
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
// (c) Anika Henke. | |
// https://github.com/selfthinker/dokuwiki_template_writr/blob/master/js/skip-link-focus-fix.js | |
(function($) { | |
jQuery.extend(jQuery.expr[':'], { | |
focusable: function(el, index, selector){ | |
var $element = $(el); | |
return $element.is(':input:enabled, a[href], area[href], object, [tabindex]') && !$element.is(':hidden'); | |
} | |
}); | |
function focusOnElement($element) { | |
if (!$element.length) { | |
return; | |
} | |
if (!$element.is(':focusable')) { | |
// add tabindex to make focusable and remove again | |
$element.attr('tabindex', -1).on('blur focusout', function () { | |
$(this).removeAttr('tabindex'); | |
}); | |
} | |
$element.focus(); | |
} | |
$(document).ready(function(){ | |
// if there is a '#' in the URL (someone linking directly to a page with an anchor) | |
if (document.location.hash) { | |
focusOnElement($(document.location.hash)); | |
} | |
// if the hash has been changed (activation of an in-page link) | |
$(window).bind('hashchange', function() { | |
var hash = "#"+window.location.hash.replace(/^#/,''); | |
focusOnElement($(hash)); | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment