193

I have a sliding panel set up on my website.

When it finished animating, I set the hash like so

function() {
   window.location.hash = id;
}

(this is a callback, and the id is assigned earlier).

This works good, to allow the user to bookmark the panel, and also for the non JavaScript version to work.

However, when I update the hash, the browser jumps to the location. I guess this is expected behaviour.

My question is: how can I prevent this? I.e. how can I change the window's hash, but not have the browser scroll to the element if the hash exists? Some sort of event.preventDefault() sort of thing?

I'm using jQuery 1.4 and the scrollTo plugin.

Many thanks!

Update

Here is the code that changes the panel.

$('#something a').click(function(event) {
    event.preventDefault();
    var link = $(this);
    var id = link[0].hash;

    $('#slider').scrollTo(id, 800, {
        onAfter: function() {

            link.parents('li').siblings().removeClass('active');
            link.parent().addClass('active');
            window.location.hash = id;

            }
    });
});
7
  • 2
    I assume you've tried event.preventDefault() :)
    – Marko
    Oct 6, 2010 at 6:49
  • @Marko I don't know where to place it!
    – alex
    Oct 6, 2010 at 6:52
  • Can you post the rest of your code?
    – Marko
    Oct 6, 2010 at 6:53
  • @Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
    – alex
    Oct 6, 2010 at 6:59
  • 3
    @Gareth I don't think there is a place for it, because it happens as soon as I update the hash.
    – alex
    Oct 6, 2010 at 7:03

9 Answers 9

308

There is a workaround by using the history API on modern browsers with fallback on old ones:

if(history.pushState) {
    history.pushState(null, null, '#myhash');
}
else {
    location.hash = '#myhash';
}

Credit goes to Lea Verou

7
  • 11
    Note that pushState has the side- (indented) effect of adding a state to the browser history stack. In other words, when the user clicks the back button, nothing will happen unless you also add a popstate event listener.
    – David Cook
    Sep 2, 2014 at 2:42
  • 42
    @DavidCook - We could also use history.replaceState (which, for hash changes, might make more sense) to avoid the need for a popstate event listener.
    – Jack
    Sep 6, 2014 at 0:38
  • This worked for me. I like the effect of the history change. I want to add the caveat that this will not trigger the hashchange event. That was something I had to work around.
    – Jordan
    Jul 2, 2015 at 19:17
  • 1
    warning! this will not trigger a hashchange event Oct 22, 2018 at 22:46
  • 1
    You don't need to use history.pushState to workaround this behavior. If your :target element has position: fixed it is removed from the normal flow so there's nowhere to scroll to. You may then assign the hash using window.history.hash = 'foo' and the window will stay put.
    – vhs
    Feb 1, 2019 at 8:00
56

The problem is you are setting the window.location.hash to an element's ID attribute. It is the expected behavior for the browser to jump to that element, regardless of whether you "preventDefault()" or not.

One way to get around this is to prefix the hash with an arbitrary value like so:

window.location.hash = 'panel-' + id.replace('#', '');

Then, all you need to do is to check for the prefixed hash on page load. As an added bonus, you can even smooth scroll to it since you are now in control of the hash value...

$(function(){
    var h = window.location.hash.replace('panel-', '');
    if (h) {
        $('#slider').scrollTo(h, 800);
    }
});

If you need this to work at all times (and not just on the initial page load), you can use a function to monitor changes to the hash value and jump to the correct element on-the-fly:

var foundHash;
setInterval(function() {
    var h = window.location.hash.replace('panel-', '');
    if (h && h !== foundHash) {
        $('#slider').scrollTo(h, 800);
        foundHash = h;
    }
}, 100);
3
  • 3
    This is the only solution that actually answers the question - allowing hashing without jumping
    – amosmos
    Feb 3, 2015 at 22:31
  • This really answers the question explaining the addition and deletion of the arbitrary value for the hash to avoid the jump as per default browser behaviour.
    – lowtechsun
    May 27, 2016 at 1:02
  • 3
    good solution, but weakens the OPs solution as it negates the use of bookmarking sections Jun 3, 2016 at 21:16
33

Cheap and nasty solution.. Use the ugly #! style.

To set it:

window.location.hash = '#!' + id;

To read it:

id = window.location.hash.replace(/^#!/, '');

Since it doesn't match and anchor or id in the page, it won't jump.

2
  • 4
    I had to preserve compatibility with IE 7 and some mobile versions of the site. This solution was cleanest for me. Generally I'd be all over the pushState solutions. May 2, 2014 at 17:14
  • 2
    setting the hash with: window.location.hash = '#/' + id; and then replacing it with: window.location.hash.replace(/^#\//, '#'); will prettify the url a bit -> projects/#/tab1
    – braitsch
    Nov 7, 2015 at 0:50
12

Why dont you get the current scroll position, put it in a variable then assign the hash and put the page scroll back to where it was:

var yScroll=document.body.scrollTop;
window.location.hash = id;
document.body.scrollTop=yScroll;

this should work

1
  • 2
    hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
    – matchew
    May 28, 2013 at 21:26
12

I used a combination of Attila Fulop (Lea Verou) solution for modern browsers and Gavin Brock solution for old browsers as follows:

if (history.pushState) {
    // IE10, Firefox, Chrome, etc.
    window.history.pushState(null, null, '#' + id);
} else {
    // IE9, IE8, etc
    window.location.hash = '#!' + id;
}

As observed by Gavin Brock, to capture the id back you will have to treat the string (which in this case can have or not the "!") as follows:

id = window.location.hash.replace(/^#!?/, '');

Before that, I tried a solution similar to the one proposed by user706270, but it did not work well with Internet Explorer: as its Javascript engine is not very fast, you can notice the scroll increase and decrease, which produces a nasty visual effect.

4

This solution worked for me.

The problem with setting location.hash is that the page will jump to that id if it's found on the page.

The problem with window.history.pushState is that it adds an entry to the history for each tab the user clicks. Then when the user clicks the back button, they go to the previous tab. (this may or may not be what you want. it was not what I wanted).

For me, replaceState was the better option in that it only replaces the current history, so when the user clicks the back button, they go to the previous page.

$('#tab-selector').tabs({
  activate: function(e, ui) {
    window.history.replaceState(null, null, ui.newPanel.selector);
  }
});

Check out the History API docs on MDN.

4

This solution worked for me

// store the currently selected tab in the hash value
    if(history.pushState) {
        window.history.pushState(null, null, '#' + id);
    }
    else {
        window.location.hash = id;
    }

// on load of the page: switch to the currently selected tab
var hash = window.location.hash;
$('#myTab a[href="' + hash + '"]').tab('show');

And my full js code is

$('#myTab a').click(function(e) {
  e.preventDefault();
  $(this).tab('show');
});

// store the currently selected tab in the hash value
$("ul.nav-tabs > li > a").on("shown.bs.tab", function(e) {
  var id = $(e.target).attr("href").substr(1);
    if(history.pushState) {
        window.history.pushState(null, null, '#' + id);
    }
    else {
        window.location.hash = id;
    }
   // window.location.hash = '#!' + id;
});

// on load of the page: switch to the currently selected tab
var hash = window.location.hash;
// console.log(hash);
$('#myTab a[href="' + hash + '"]').tab('show');
1

I'm not sure if you can alter the original element but how about switch from using the id attr to something else like data-id? Then just read the value of data-id for your hash value and it won't jump.

0
0

When using laravel framework, I had some issues with using a route->back() function since it erased my hash. In order to keep my hash, I created a simple function:

$(function() {   
    if (localStorage.getItem("hash")    ){
     location.hash = localStorage.getItem("hash");
    }
}); 

and I set it in my other JS function like this:

localStorage.setItem("hash", myvalue);

You can name your local storage values any way you like; mine named hash.

Therefore, if the hash is set on PAGE1 and then you navigate to PAGE2; the hash will be recreated on PAGE1 when you click Back on PAGE2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.