Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:14:59+00:00 2026-06-04T16:14:59+00:00

I used the onhashchange event to scroll automatically to an anchor link in a

  • 0

I used the onhashchange event to scroll automatically to an anchor link in a FAQ page when the user presses the back button.

It works fine except when the forward button is pressed, case in which the scrolling doesn’t work.

I haven’t been able to understand why this happens. Does anybody have an idea of why this happens?

Demo page: http://static.nemesisdesign.net/hashchange-issue/index.html (to reproduce the issue try clicking on the links on the left column, then press your back button few times, then press your forward button)

This is the part of the code which does the scrolling:

// if browser supports onhashchange event
if ("onhashchange" in window){
    $(window).bind('hashchange', function(e){
        e.preventDefault();
        var href = (location.hash != '') ? location.hash : '#container'
        scrollToFaq(href);
        return false;
    });
}

scrollToFaq = function(href){
    // scroll
    $('html, body').animate(
        // scroll to clicked item
        {scrollTop: $(href).offset().top},
        400, // duration in ms
        function(){
            if(href != '#container'){
                location.hash = href.replace('#',''); // update hash
            }
            // show back button if necessary
            if($(window).scrollTop() > 60){
                // onclick back to top button
                $('#back-top').fadeIn();
            }
        }
    );
}

// when clicking on a faq menu item
$('#faqmenu a').click(function(e){
    // prevent default behaviour
    e.preventDefault();
    // cache href attribute
    scrollToFaq($(this).attr('href'));
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:15:01+00:00Added an answer on June 4, 2026 at 4:15 pm

    It seems that when using the back & forward buttons the browser does its default scroll action regardless of the e.preventDefault(); in the onhashchange event handler.

    Basically this won’t stop the browser from jumping when using the back and forward buttons:

    if ("onhashchange" in window){
        $(window).bind('hashchange', function(e){
            e.preventDefault();
            return false;
        });
    }
    

    The simplest hack that I could think of is using some inexistent id in the hash (such that it doesn’t jump).

    For example for referencing <div id="a"> you can use <a href="#xxx_a">. Then all you have to do is strip the xxx_ part with javascript.

    But the disadvantage is that you break the default behavior of the links for javascript disabled users, and browsers that don’t support onhashchange.

    You can check out this jsFiddle to see a working example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the following code to display the event details fetched from the database.
I used the following link to download the latest Xampp in order to install
used to check user input like username not a login account.
I used CouchDB 1.1.1 with self-signed certs fine for some time. Then, I purchased
i used the user's valid access token to identify the user... with this: https://graph.facebook.com/me?fields=id&access_token=VALID_ACCESS_TOKEN
I used following command on windows xp and its working perfectly fine. rundll32.exe url.dll,FileProtocolHandler
Used treeview.SelectedNode to select a child node. How to invoke treeview.AfterSelect event when a
I used jQuery to set hover callbacks for elements on my page. I'm now
used image as like map , user can zoom in/out this image.geting x and
I used twitter Oauth and it worked perfectly fine.I was able to login using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.