Chrome Anchor Link Not Working Fix
One of my single page sites that had been live for months with no changes suddenly had the menu stop functioning in Chrome only. I can’t find an exact reason for the break, but I did find a cross-browser JavaScript solve that fixes the issue nicely. Just add the snippet to your site and the links should work automatically. Note that this fix requires jQuery but can be converted to VanillaJS.
/* Navigation Fix -----------------------------------------------------*/ $(function() { $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html, body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); });
Someone reached out to me today for help as the fix broke their WordPress site. If you’re using WordPress, this version uses the compatibility version of jQuery they provide out of the box. Be sure to place it in a javascript file and not your functions.php. Rui was kind enough to confirm the fix also works for Drupal 7. Â
/* WordPress Navigation Fix -----------------------------------------------------*/ jQuery(function ($) { $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html, body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); });
Alexis Hawke
Thank you so much for this fix – I had two sites with the same issue – only occurred on single-page sites where an anchor was used. Your fix sure was a life-saver! Thank you.
Greg
Glad it helped! I still can’t find the root cause of the issue which is frustrating me to no end.
Greg
THANK YOU!!! I was trying to figure this out and your solution worked like a charm! Very much appreciate you posting!
Greg
Glad it helped, Alexis!
Cavallone
Thank you! It worked for me even if i don’t understand what script does …:-)
Andre Castro
Thank you! It worked fine!
Greg
Glad it helped, Andre!
Pete
Hey it still doesn’t seem to work on my android chrome. Any ideas where the script should be placed?
Greg
Hi Pete,
You can put it anywhere in your code after your reference to the jquery library. I just tested on Android Chrome and it’s working for me. If you can share a link, I’ll be happy to try and help you troubleshoot.
Stephen
Excellent solution. Much appreciated.
James
Thank you guy ! it roXx… you save my dev 🙂
Greg
Glad it helped, James!
Jose Diego Diaz
Hi. i use your script in my WordPress Site and fix the “Menu anchor links” but in the “#productos-y-servicios” the porfolio images dissapear. I remove your script and the images come back but the
anchor links fail. What coudl be? (I’m not so good with scripts). Thanks a lot
Caio Carvalho
Hey there!
The js code really fixed my menu problem, but another links still dont jumping to content.
I already tried so many forums, codes and change js properties but it just mess up w/ my website.
I use the OneEngine wordpress theme, and i have no support.
What could it be? If someone has a code just to paste it, i would be SOOO greatfull.
Greg
Hey Caio,
I looked at your site, and if you’re referring to the “View more” link, there’s no anchor tag in the URL present on it so the script won’t do anything. If you’re not using javascript to jump to another point, you need to add an anchor tag to the link.
Let me know if that helps.
Caio Carvalho
Actually there is a scroll to in portfolio section too, but in javascript it’s all set, with anchor links.
In FF, IE and Opera works fine. Maybe i’m missing something…
Kike Vesga
Thanks man, saved the day!
Greg
Glad it helped!
Filipe Apóstolo
I tried to fix with your script but still doesn’t work. My Single page with ancors menu works fine in FF; IE and Opera and Safari;
I wrote the script inside the head and after calling jquery script. What is missing?
Greg
Hi Filipe,
What’s the URL to your site?
Vadim
Hello,
I have a button on the first slide of my homepage that has an anchor that link to a form down the page. The button works on all the browsers except Chrome. I pasted your code into the “Custom JavaScript” section of the theme options but it still does not work. Please help.
Robert
I have tried to use this code on my word press website and it does not fix this issue. Pease help.
Greg
Robert, what’s your site URL? It’s hard to debug anything without any information. 🙂
simon
hi thx for your help!
maybe its super stupid , but
where should I insert the code snippet?
is there a specific script in the editor of wordpress template or should i create a just new one?
sry if its stupid
best regards
simon
i paste it in the functions script and it works – awesome you saved my day too 🙂
Greg
Glad you were able to figure it out Simon!
simon
a short question – the scroll effect is working in chrome 🙂 but now the code is shown as text on top of the page – i tried to put the code snippet in but then the page is not workng an gives an error cannot modifiy header. Do you have maby another tip?
thx best regards simon
Greg
Simon, by functions script, did you mean functions.php? If so, you need to add it to a javascript file, not the functions.php as the code is javascript, not php.
Peter
THANKS! It’s been killing me for more than a year now, with clients complaining
Greg
Glad it helped, Peter!
Mark
Working Super Fine …Thanks a ton ..!
Greg
Glad it helped, Mark!
Rob
When I paste it in the functions.php it breaks the site. Any ideas? url: http://www.stroomindepijp.nl
Greg
Hi Rob, if you’re using the snippet as-is, you need to put it into a javascript file, not functions.php as it’s not PHP code. I quickly looked at your site and your best bet would be to put it in this file:
http://stroomindepijp.nl/wp-content/themes/rhythm/js/all.js
Rob
Great, it worked! Thanks a lot for the help!
Greg
Glad it worked for you!
David
Great!! Thank you!!!
Greg
Glad it helped David!
Lawrence
Hey there, really love the solution but it isn’t working on my wordpress site! I even tried both code snippets. You can check my site here: llddesign.com
I appreciate any insight you can provide
Greg
Hi Lawrence, your site is showing maintenance mode so I can’t check it. Did you put the second snippet in a javascript file?
Rui Batista
Hi and thanks so much for this fix. The WordPress solution also works perfectly in Drupal (version 7)
Been looking for a way to fix the end of tags function as anchors and thanks to you it is solved.
Greg
Glad it worked for you, Rui! Thank you for confirming that it works for Drupal 7 too. I’ll update the post in case anyone else with Drupal 7 runs into the issue. 🙂
PMack
Thanks Man! Fixed my problem
Greg
Glad it helped PMack!
Kati
Thank you sooo much for the code!!! Now the navigation works again perfectly…
Greg
Glad it helped Kati!
Anna
OMG, thank you so very much for this fix! <3 <3 This was driving me crazy.
Greg
Glad it helped Anna!
collidingstar
I was hoping this would work but so far no luck.
Tried in a few places.
The Theme was update but I didn’t create the site so I can’t download it.
http://levilowreyofficial.com/
Greg
Did you figure it out already or do you still need help?
Milen
Dear Greg,
I just want to tell a huge THANK YOU! for your script, which solved a similar issue I had with my website. I spent hours reading articles and replacing files without any success. I almost gave up on this and then I found your fix. Now everything works just great, both on desktop and mobile! Awesome work, thanks a lot!
Have a great day,
Milen
Greg
Glad it helped, Milen!
Feli Moya
a million thanks, I had already thrown in the towel
Greg
Glad it helped, Feli!
Harjinder
My a href is not empty. So what should i use? It tag start a href=”somelink” tag close NIGHT It works on all browsers except Chrome.
WWE Fan
In one of my website links suddenly stopped working but only on Chrome.
I already have tried this solution. but this solution is for href containing # i guess. Mine href’s are not empty or contains #. Help me out please because client says he will create a dispute if it doesn’t work.
Greg
Hi Harjinder,
The script is designed to fix anchor tags, which use the # in it. From my testing, regular links haven’t been affected by the issue and is probably being affected by something else.
If you can post a URL or sample code, I can try to help.
WWE Fan
Here it is http://www.whizdomwebsolutions.com/music/ It’s working everywhere else except Chrome.
Greg
I just tested the links in the menu and they open for me in Chrome 73.0.3683.86. Which links aren’t working specifically?
WWE Fan
WooCommerce Products Carousel under Exclusive Cover Designs and Popular Cover Designs
Greg
You have errors on your page so it’s hard to debug fully, but your slider is throwing warnings for a touchstart here when you click on a link on that page:
onTouchStart @ jquery.bxxslider.js?ver=1.0.7:1212
You will probably need an updated slider script to fix the issue. Reach out to the theme author or try a newer version of the slider script. BXSlider is now on version 4 (https://github.com/stevenwanderski/bxslider-4) and your site appears to be using version 1.
You could try also this fix since you shouldn’t need ontouchstart on desktop :
https://stackoverflow.com/questions/45164761/bxslider-disabling-touchenabled-for-desktop-browsers
Good luck!
WWE Fan
Fixed, Thanks a lot! It was because of ontouchscreen.
Greg
Glad to help!
Shawn
Thank you! My problem was in Chrome and only with links to ID’s within the same page. Sometimes they would work, usually they would move a bit (maybe 40px) and then stick there. A second click on the link would get them to act as desired and move to the referenced div. Other browsers worked flawlessly. Strange, but this snippet has done the job. Thank you so much for your help. Hope you have a great day, you surely brightened mine!