How can I update window.location.hash without jumping the document?
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;
);
);
javascript jquery hash scrollto
|
show 2 more comments
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;
);
);
javascript jquery hash scrollto
2
I assume you've triedevent.preventDefault()
:)
– Marko
Oct 6 '10 at 6:49
@Marko I don't know where to place it!
– alex
Oct 6 '10 at 6:52
Can you post the rest of your code?
– Marko
Oct 6 '10 at 6:53
@Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
– alex
Oct 6 '10 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 '10 at 7:03
|
show 2 more comments
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;
);
);
javascript jquery hash scrollto
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;
);
);
javascript jquery hash scrollto
javascript jquery hash scrollto
edited Oct 6 '10 at 7:02
alex
asked Oct 6 '10 at 6:46
alexalex
344k171769915
344k171769915
2
I assume you've triedevent.preventDefault()
:)
– Marko
Oct 6 '10 at 6:49
@Marko I don't know where to place it!
– alex
Oct 6 '10 at 6:52
Can you post the rest of your code?
– Marko
Oct 6 '10 at 6:53
@Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
– alex
Oct 6 '10 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 '10 at 7:03
|
show 2 more comments
2
I assume you've triedevent.preventDefault()
:)
– Marko
Oct 6 '10 at 6:49
@Marko I don't know where to place it!
– alex
Oct 6 '10 at 6:52
Can you post the rest of your code?
– Marko
Oct 6 '10 at 6:53
@Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
– alex
Oct 6 '10 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 '10 at 7:03
2
2
I assume you've tried
event.preventDefault()
:)– Marko
Oct 6 '10 at 6:49
I assume you've tried
event.preventDefault()
:)– Marko
Oct 6 '10 at 6:49
@Marko I don't know where to place it!
– alex
Oct 6 '10 at 6:52
@Marko I don't know where to place it!
– alex
Oct 6 '10 at 6:52
Can you post the rest of your code?
– Marko
Oct 6 '10 at 6:53
Can you post the rest of your code?
– Marko
Oct 6 '10 at 6:53
@Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
– alex
Oct 6 '10 at 6:59
@Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
– alex
Oct 6 '10 at 6:59
3
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 '10 at 7:03
@Gareth I don't think there is a place for it, because it happens as soon as I update the hash.
– alex
Oct 6 '10 at 7:03
|
show 2 more comments
9 Answers
9
active
oldest
votes
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
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
9
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 '14 at 2:42
24
@DavidCook - We could also usehistory.replaceState
(which, for hash changes, might make more sense) to avoid the need for apopstate
event listener.
– Jack
Sep 6 '14 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 thehashchange
event. That was something I had to work around.
– Jordan
Jul 2 '15 at 19:17
warning! this will not trigger ahashchange
event
– santiago arizti
Oct 22 '18 at 22:46
|
show 1 more comment
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);
2
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 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 '16 at 1:02
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
add a comment |
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
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
2
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.
– Eric Goodwin
May 2 '14 at 17:14
1
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 '15 at 0:50
add a comment |
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
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
1
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
1
Better usewindow.scroll()
to set it back...
– vsync
Apr 2 '14 at 14:55
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3870057%2fhow-can-i-update-window-location-hash-without-jumping-the-document%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
9
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 '14 at 2:42
24
@DavidCook - We could also usehistory.replaceState
(which, for hash changes, might make more sense) to avoid the need for apopstate
event listener.
– Jack
Sep 6 '14 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 thehashchange
event. That was something I had to work around.
– Jordan
Jul 2 '15 at 19:17
warning! this will not trigger ahashchange
event
– santiago arizti
Oct 22 '18 at 22:46
|
show 1 more comment
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
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
9
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 '14 at 2:42
24
@DavidCook - We could also usehistory.replaceState
(which, for hash changes, might make more sense) to avoid the need for apopstate
event listener.
– Jack
Sep 6 '14 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 thehashchange
event. That was something I had to work around.
– Jordan
Jul 2 '15 at 19:17
warning! this will not trigger ahashchange
event
– santiago arizti
Oct 22 '18 at 22:46
|
show 1 more comment
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
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
answered Feb 4 '13 at 15:44
Attila FulopAttila Fulop
4,64122841
4,64122841
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
9
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 '14 at 2:42
24
@DavidCook - We could also usehistory.replaceState
(which, for hash changes, might make more sense) to avoid the need for apopstate
event listener.
– Jack
Sep 6 '14 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 thehashchange
event. That was something I had to work around.
– Jordan
Jul 2 '15 at 19:17
warning! this will not trigger ahashchange
event
– santiago arizti
Oct 22 '18 at 22:46
|
show 1 more comment
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
9
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 '14 at 2:42
24
@DavidCook - We could also usehistory.replaceState
(which, for hash changes, might make more sense) to avoid the need for apopstate
event listener.
– Jack
Sep 6 '14 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 thehashchange
event. That was something I had to work around.
– Jordan
Jul 2 '15 at 19:17
warning! this will not trigger ahashchange
event
– santiago arizti
Oct 22 '18 at 22:46
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
This is the better answer, in my opinion.
– Greg Annandale
Jun 10 '14 at 21:15
9
9
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 '14 at 2:42
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 '14 at 2:42
24
24
@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 '14 at 0:38
@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 '14 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 '15 at 19:17
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 '15 at 19:17
warning! this will not trigger a
hashchange
event– santiago arizti
Oct 22 '18 at 22:46
warning! this will not trigger a
hashchange
event– santiago arizti
Oct 22 '18 at 22:46
|
show 1 more comment
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);
2
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 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 '16 at 1:02
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
add a comment |
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);
2
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 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 '16 at 1:02
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
add a comment |
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);
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);
edited Mar 24 '11 at 5:06
answered Mar 24 '11 at 4:56
Derek HunzikerDerek Hunziker
10.4k34899
10.4k34899
2
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 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 '16 at 1:02
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
add a comment |
2
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 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 '16 at 1:02
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
2
2
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 at 22:31
This is the only solution that actually answers the question - allowing hashing without jumping
– amosmos
Feb 3 '15 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 '16 at 1:02
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 '16 at 1:02
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
good solution, but weakens the OPs solution as it negates the use of bookmarking sections
– Samus
Jun 3 '16 at 21:16
add a comment |
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
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
2
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.
– Eric Goodwin
May 2 '14 at 17:14
1
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 '15 at 0:50
add a comment |
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
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
2
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.
– Eric Goodwin
May 2 '14 at 17:14
1
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 '15 at 0:50
add a comment |
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.
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.
answered Jun 14 '13 at 12:34
Gavin BrockGavin Brock
4,32012233
4,32012233
2
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
2
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.
– Eric Goodwin
May 2 '14 at 17:14
1
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 '15 at 0:50
add a comment |
2
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
2
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.
– Eric Goodwin
May 2 '14 at 17:14
1
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 '15 at 0:50
2
2
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
+1 I think yours is the most effective one :)
– Sisir
Apr 9 '14 at 11:57
2
2
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.
– Eric Goodwin
May 2 '14 at 17:14
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.
– Eric Goodwin
May 2 '14 at 17:14
1
1
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 '15 at 0:50
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 '15 at 0:50
add a comment |
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
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
1
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
1
Better usewindow.scroll()
to set it back...
– vsync
Apr 2 '14 at 14:55
add a comment |
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
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
1
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
1
Better usewindow.scroll()
to set it back...
– vsync
Apr 2 '14 at 14:55
add a comment |
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
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
answered Apr 13 '11 at 15:02
user706270user706270
13313
13313
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
1
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
1
Better usewindow.scroll()
to set it back...
– vsync
Apr 2 '14 at 14:55
add a comment |
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
1
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
1
Better usewindow.scroll()
to set it back...
– vsync
Apr 2 '14 at 14:55
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
That's so crazy... it just might work. Update: It works!
– anewcomer
Sep 12 '12 at 14:02
1
1
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
hm, this was working for me for awhile, but sometime in the last few months this has stopped working on firefox....
– matchew
May 28 '13 at 21:26
1
1
Better use
window.scroll()
to set it back...– vsync
Apr 2 '14 at 14:55
Better use
window.scroll()
to set it back...– vsync
Apr 2 '14 at 14:55
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Oct 14 '15 at 15:50
aldemarcalazansaldemarcalazans
52168
52168
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Oct 31 '13 at 23:16
Jon BJon B
695
695
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Apr 28 '18 at 14:04
Mike VallanoMike Vallano
14623
14623
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Aug 29 '18 at 0:54
AndrewAndrew
4,559945101
4,559945101
add a comment |
add a comment |
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');
add a comment |
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');
add a comment |
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');
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');
answered Nov 14 '18 at 10:24
Rohit DhimanRohit Dhiman
1,830727
1,830727
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3870057%2fhow-can-i-update-window-location-hash-without-jumping-the-document%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I assume you've tried
event.preventDefault()
:)– Marko
Oct 6 '10 at 6:49
@Marko I don't know where to place it!
– alex
Oct 6 '10 at 6:52
Can you post the rest of your code?
– Marko
Oct 6 '10 at 6:53
@Marko Ivanovski I don't think it is relevant, but I'll see what I can do.
– alex
Oct 6 '10 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 '10 at 7:03