Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move browser to anchor location without causing the page to refresh

I have a web page which reloads some data using a jquery post every 30 seconds, the jquery post will will retrieve the anchors in a tab form so when the jquery does the refresh the anchor is always put to the first anchor.

Let's say I have #anchor1, #anchor2 and #anchor3. If the user is viewing #anchor2 and jquery reloads, it defaults to #anchor1. Before the jquery is done I check to see which anchor the viewer is using, and then after jquery has finished I then select the anchor that they were looking at by using the code below

document.location.href= "#anchor2";

When I use the above code it obviously causes the page to refresh so it then does the initial document.ready function to retrieve the data, so therefore the timer never gets used, it just instead permanently keeps reloading the jquery, doing it every second or so instead of using the javascript timer.

How can I direct the user to the anchor that the user was looking at before the ajax reload without causing the page to refresh.

Thanks for any help you can provide.

like image 796
Boardy Avatar asked Jul 07 '12 21:07

Boardy


1 Answers

Use the document.location.hash property:

document.location.hash = "#anchor2";
like image 159
Alex Avatar answered Sep 29 '22 11:09

Alex