Jigna Jain
Jigna Jain

Reputation: 255

Href tag not working in chrome but working in other browser

I'm working on designing website but facing a issue that href # tags are not working in chrome but working in firefox

<ul class="navigation" style="margin-top: 75px;">
   <li><a class="scroll-to" href="#section-1">Home</a></li>
   <li><a class="scroll-to" href="#section-2">About Us</a></li>
   <li><a class="scroll-to" href="#section-4">Products</a></li>
   <li><a class="scroll-to" href="#section-5">Clients</a></li>
   <li><a class="scroll-to" href="#section-6">Team</a></li>
   <li><a class="scroll-to" href="#section-7">Contact Us</a></li>
</ul>


<section id="section-1" class="banner-container color-light center nav-trigger">

I am not sure where its going wrong

Upvotes: 1

Views: 10088

Answers (5)

zb_
zb_

Reputation: 1

For me, it was the href that redirected to pages with hashtags, so I did this workaround:

$('body').on('click', 'a[href*="#"]:not([href="#"])', function(e) {
    if($(this).attr('href').indexOf('http') > -1 && /chrome/i.test( navigator.userAgent) ){
        e.preventDefault();
        window.location.href = $(this).attr('href');
        window.location.reload();
        return false;
    }
});

Upvotes: 0

Jedsada Saengow
Jedsada Saengow

Reputation: 291

You can try follow this. https://www.gregoryvarghese.com/chrome-anchor-link-not-working-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;
       }
     }
   });
 });

Upvotes: 1

Sanjyot Shah
Sanjyot Shah

Reputation: 225

I guess its a bug in certain versions of Chrome, this workaround did the trick for me, good luck! -

$(document).ready(function () {
    var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
    if (window.location.hash && isChrome) {
        setTimeout(function () {
            var hash = window.location.hash;
            window.location.hash = "";
            window.location.hash = hash;
        }, 300);
    }
});

Upvotes: 0

wasimv09
wasimv09

Reputation: 146

Make sure you have enough height for each div so that page can scroll. Without page scroll you can't see changes, because nothing wrong with your code.

<ul>
   <li><a href="#section-1">Home</a></li>
   <li><a href="#section-2">About Us</a></li>
   <li><a href="#section-4">Products</a></li>
   <li><a href="#section-5">Clients</a></li>
   <li><a href="#section-6">Team</a></li>
   <li><a href="#section-7">Contact Us</a></li>
</ul>
<section style="height:500px" id="section-1">Home</section>
<section style="height:500px" id="section-2">About Us</section>
<section style="height:500px" id="section-4">Products</section>
<section style="height:500px" id="section-5">Clients</section>
<section style="height:500px" id="section-6">Team</section>
<section style="height:500px" id="section-7">Contact Us</section>

Upvotes: 0

user736893
user736893

Reputation:

The following works fine for me in Chrome 62. Are you closing your section tag? Are you sure there is enough height that it would actually scroll?

section {
   padding: 100px;
   border: 1px solid blue;
}
<ul>
    <li><a href="#section-1">Home</a></li>
    <li><a href="#section-2">About Us</a></li>
    <li><a href="#section-4">Products</a></li>
    <li><a href="#section-5">Clients</a></li>
    <li><a href="#section-6">Team</a></li>
    <li><a href="#section-7">Contact Us</a></li>
</ul>
<section id="section-1">Home</section>
<section id="section-2">About Us</section>
<section id="section-4">Products</section>
<section id="section-5">Clients</section>
<section id="section-6">Team</section>
<section id="section-7">Contact Us</section>

Upvotes: 2

Related Questions