Call javascript function based on Fragment ID
-
21-07-2021 - |
Question
Please dont ask why I would want to do this, I just want to know if it's possible to call a javascript function if and only if a specific fragment ID is present. Thanks :)
(Yes this links with my previous question, but it was too dissimilar to append on to it) if this is not possible, is there another way of calling a javascript function based on the URL.
Solution
Yes. Of course it is. You can get the fragment, check it's value and then call a function if it matches.
if (window.location.hash == "#myfragment") {
myFunction();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow