Doc Green
Doc Green

Reputation: 13

JS works fine in Chrome but not in FireFox

It's a simple window scroll that triggers when the user clicks on a nav item but for the life of me I cant seem to figure out why FF wont recognise it, I've read a similar Q&A's but they only recommend defining var's first which I've done, any help would be appreciated.

Heres the code:

$("#myNavbar a").on('click', function(){
var hash = this.hash;

//make sure this.hash has a value
if (hash !== ""){
    //prevent default anchor click behavior
    event.preventDefault();


    //use jQuerys animate() method to add smooth scroll
    $('html, body').animate({
        scrollTop: $(hash).offset().top
    }, 800, function(){

        window.location.hash = hash;
    });
} // end of if
});

Upvotes: 1

Views: 358

Answers (1)

noppa
noppa

Reputation: 4076

This "works" in Chrome because Chrome implements the non-standard Window.event property.

As noted in the comments, you should use the event argument that is provided by jQuery event handler instead.

$("#myNavbar a").on('click', function(event){
    ...
    event.preventDefault();
    ...
});

Upvotes: 1

Related Questions