Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  122] [ 5]  / answers: 1 / hits: 81242  / 15 Years ago, fri, february 19, 2010, 12:00:00

Is it possible to remove the hash from window.location without causing the page to jump-scroll to the top? I need to be able to modify the hash without causing any jumps.



I have this:



$('<a href=#123>').text('link').click(function(e) {
e.preventDefault();
window.location.hash = this.hash;
}).appendTo('body');

$('<a href=#>').text('unlink').click(function(e) {
e.preventDefault();
window.location.hash = '';
}).appendTo('body');


See live example here: http://jsbin.com/asobi



When the user clicks 'link' the hash tag is modified without any page jumps, so that's working fine.



But when the user clicks 'unlink' the has tag is removed and the page scroll-jumps to the top. I need to remove the hash without this side-effect.


More From » jquery

 Answers
34

I believe if you just put in a dummy hash it won't scroll as there is no match to scroll to.



<a href=#A4J2S9F7>No jumping</a>


or



<a href=#_>No jumping</a>


# by itself is equivalent to _top thus causes a scroll to the top of the page


[#97536] Monday, February 15, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;