Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript and anchors

Status
Not open for further replies.

faxof

Programmer
Dec 5, 2001
272
GB
i am using normal css and html.

i link from one page to another using an anchor (so i can link to a specific part of the page)

is there a way, using javascript or otherwise, that i can make the link go to the new page (at the top) and then automatically scroll down to the anchor?

i think that will look wicked, any ideas?

cheers
fax
 
You'd need to put this script on the target page. You'd check to see if "location.hash" was populated, and if so, use something like the "scrollIntoView" method.

I say "like", because the scrollIntoView method is IE-only AFAIK, so you'd need to find a cross-browser way (assuming this is an internet page, not an intranet page for IE-only users).

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I have not tried it myself but my approach would go like this.
Rather than using bookmark tags I would just pass a value on the URL stating the intended destination to scroll to.
In the loading page I would read that value, test for the current scroll position of that tag on the page then use a loop to increment the scrolTop property until it reaches the tags position.
Getting fancy I would even do some highlighting at the beginning of the data for that section, possibly flashing it different colors or highlighting a couple times to get attention then setting it to normal or highlighted.
The big reason for doing that is if the section of the page you want to move to is not able to scroll to the top of the page (like it is too close to the bottom to be able to position it at the top because there is not enough space below it) then you want to catch the viewers attention as to the content they are being directed to.

You would have to check the scroll height of the page, the height of the viewable window and the position of the tag you want to scroll to in order to come up with the number of pixels to scroll down. If the tag was only 100px from the bottom of the page and the browser window was 800px high you obviously cannot scoll the screen to set the tag at the top of the screen, you can only scroll until the bottom of the scroll window is even with the bottom of the browser screen. So you have to figure out how many pixels you can move before the tag reaches the top limit or the bottom of the scroll window has reached the bottom of the display window whichever comes first and use that for your increment loop.

Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top