jgallen23 / toc

Table of Contents Plugin
http://projects.jga.me/toc/
MIT License
531 stars 111 forks source link

Anchor link with complete URL #51

Open Anke opened 9 years ago

Anke commented 9 years ago

Hi,

thanks for this neat script. Using a CMS I need to add the URL to the anchor name in the link, so I changed line 70 from .attr('href', '#' + anchorName) to .attr('href', window.location + '#' + anchorName).

EDIT: The above change of code seemed to be working well at first, but now I realise that something isn't working well. Clicking on another link now, the anchor is being added to the URL - http://mydomain.com/mypage.html#toc38#toc19. Refreshing the page adds yet another anchor to the URL etc.

Did I trigger this behavior by adding window.location? Is there a solution for this?

I'd very much appreciate your help, Anke

FlowinBeatz commented 8 years ago

Hi @Anke, did you solve this issue in any way?

Anke commented 8 years ago

Uh, thank you for your reply, but honestly - I don't know. I might have, but might just as well have implemented another script. Sorry.

kingsloi commented 6 years ago

Really hacky solution, but I wanted something simple rather than sinking any time into it. I ended up with:

    $('.toc').on('click', 'a', function() {
        var fixedUrl = window.location.href.split('#')[0] + $(this).attr('href');
        setTimeout(function() {
            window.location.replace(fixedUrl);
        }, 600);
    });

The timer is for the url to update once the smooth scroll has finished + a lil bit of padding. Awkward, but works. @030media @Anke

FlowinBeatz commented 6 years ago

Thanks a lot, I will give it a try once I have to use a TOC again!

Anke commented 6 years ago

Great, I'll try that. Thanks!