alicelieutier/smoothScroll

preventDefault() on anchor click event does nothing

Closed this issue · 4 comments

fenok commented

Smoothscroll always changes location.hash. It should consider event.defaultPrevented. Costed me a lot of time while debugging complex project.

Hi @fenok, thanks for your feedback.
I am not sure what you mean by "it should consider event.defaultPrevented". Can you explain your problem a bit more?

fenok commented

Sure. I'm talking about this and this pieces of code. They change location.hash regardless of whether event.preventDefault() has been called on the click event.
In my case, that breaks hash-based routing, which is used due to Cordova restrictions. Anchors (hashes) are translated into query params (like 'path/to/page#anchorname' => '#path/to/page?anchor=anchorname') by internal router, and scroll behaviour is scripted there.
Therefore I would like to be able to prevent default browser behaviour (hash change and scroll), which is impossible with smoothScroll. This could be fixed by checking event.defaultPrevented before changing location.hash (and before scrolling, of course). This would also make smoothScroll consistent with native 'default preventable' links.

Ok, this makes sense. Do you want to submit a PR for this?

fenok commented

That sounded like a good opportunity to learn how they works, so I did =)