ndabas / toc

Table of Contents jQuery Plugin - A minimal, tiny jQuery plugin that will generate a table of contents, drawing from headings on the page.
https://ndabas.github.io/toc/
Apache License 2.0
84 stars 24 forks source link

Relative anchor fails when base href is set on page. #6

Closed prasad83 closed 8 years ago

prasad83 commented 8 years ago

Consider the http://site.tld/page is being viewed and is configured.

TOC link <a href="#inside-page"> would result to http://site.tld#inside-page - WRONG.

Having options... base would be good.


$("<li/>").appendTo(stack[0]).append(
$("<a/>").text(elem.text()).attr("href", (thisOptions.base ? thisOptions.base : '') + "#" + elem.attr("id"));
);

In the example page stated above, I would use

jQuery('#toc').toc({base: '/page'});
ndabas commented 8 years ago

This isn't an issue with this library, per se.

You can easily handle this case with some code like this:

jQuery("#toc").toc().find("a").attr("href", function (i, a) { return "/page" + a; });