yaml-focusfix.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * "Yet Another Multicolumn Layout" - YAML CSS Framework
  3. *
  4. * (en) Workaround for IE8 und Webkit browsers to fix focus problems when using skiplinks
  5. * (de) Workaround für IE8 und Webkit browser, um den Focus zu korrigieren, bei Verwendung von Skiplinks
  6. *
  7. * @note inspired by Paul Ratcliffe's article
  8. * http://www.communis.co.uk/blog/2009-06-02-skip-links-chrome-safari-and-added-wai-aria
  9. * Many thanks to Mathias Schäfer (http://molily.de/) for his code improvements
  10. *
  11. * @copyright Copyright 2005-2012, Dirk Jesse
  12. * @license CC-BY 2.0 (http://creativecommons.org/licenses/by/2.0/),
  13. * YAML-CDL (http://www.yaml.de/license.html)
  14. * @link http://www.yaml.de
  15. * @package yaml
  16. * @version 4.0+
  17. * @revision $Revision: 617 $
  18. * @lastmodified $Date: 2012-01-05 23:56:54 +0100 (Do, 05 Jan 2012) $
  19. */
  20. (function () {
  21. var YAML_focusFix = {
  22. skipClass : 'ym-skip',
  23. init : function () {
  24. var userAgent = navigator.userAgent.toLowerCase();
  25. var is_webkit = userAgent.indexOf('webkit') > -1;
  26. var is_ie = userAgent.indexOf('msie') > -1;
  27. if (is_webkit || is_ie) {
  28. var body = document.body,
  29. handler = YAML_focusFix.click;
  30. if (body.addEventListener) {
  31. body.addEventListener('click', handler, false);
  32. } else if (body.attachEvent) {
  33. body.attachEvent('onclick', handler);
  34. }
  35. }
  36. },
  37. trim : function (str) {
  38. return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  39. },
  40. click : function (e) {
  41. e = e || window.event;
  42. var target = e.target || e.srcElement;
  43. var a = target.className.split(' ');
  44. for (var i=0; i < a.length; i++) {
  45. var cls = YAML_focusFix.trim(a[i]);
  46. if ( cls === YAML_focusFix.skipClass) {
  47. YAML_focusFix.focus(target);
  48. break;
  49. }
  50. }
  51. },
  52. focus : function (link) {
  53. if (link.href) {
  54. var href = link.href,
  55. id = href.substr(href.indexOf('#') + 1),
  56. target = document.getElementById(id);
  57. if (target) {
  58. target.setAttribute("tabindex", "-1");
  59. target.focus();
  60. }
  61. }
  62. }
  63. };
  64. YAML_focusFix.init();
  65. })();