From 62827d68cac988c09cac7cda9fb7d56f40461a65 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 13 Nov 2013 14:29:03 +0100 Subject: [PATCH] [doc toc script] More accurate active link highlighting --- doc/activebookmark.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/activebookmark.js b/doc/activebookmark.js index 88c879cf42..407282d02c 100644 --- a/doc/activebookmark.js +++ b/doc/activebookmark.js @@ -3,6 +3,7 @@ document.createElement("section"); document.createElement("article"); (function() { + if (!window.addEventListener) return; var pending = false, prevVal = null; function updateSoon() { @@ -41,8 +42,16 @@ document.createElement("article"); } } - if (window.addEventListener) { - window.addEventListener("scroll", updateSoon); - window.addEventListener("load", updateSoon); - } + window.addEventListener("scroll", updateSoon); + window.addEventListener("load", updateSoon); + window.addEventListener("hashchange", function() { + setTimeout(function() { + var hash = document.location.hash, found = null, m; + var marks = document.getElementById("nav").getElementsByTagName("a"); + for (var i = 0; i < marks.length; i++) + if ((m = marks[i].href.match(/(#.*)/)) && m[1] == hash) { found = i; break; } + if (found != null) for (var i = 0; i < marks.length; i++) + marks[i].className = i == found ? "active" : ""; + }, 300); + }); })();