Forum Moderators: open

Message Too Old, No Replies

Script to stop page scrolling when clicking in an anchor link

onclick anchor roll up page

         

aidem

10:28 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



Hi everyone,
Brand new member and total js iliterate here...
i would like to know what script is used to avoid the annoying scroll up of a page when clicking in an anchor.

I have this page with a Ilayer which display the content trough clicking in different anchor names (blue menu). So far couldn't find the right script to add to the onclick event so that the page won't scroll up when an anchor name is clicked.

i know this is basic for most of you guys so any help is really appreciated.

Thanks

aidem

[edited by: encyclo at 1:38 am (utc) on April 1, 2007]
[edit reason] no URLs please, see TOS [webmasterworld.com] [/edit]

music_man

4:13 am on Apr 8, 2007 (gmt 0)

10+ Year Member



Try:

<a href="javascript:void(0);" onclick="action()">Link</a>

Achernar

1:59 pm on Apr 9, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



aidem, I didn't have the time to look at a working solution at the time of your post. I've kept a copy of the url, and here is a solution:

Add this javascript to the top of your document:

var s3; 
function sTo(a) {
if (!a) return true;
if (!s3) s3=document.getElementById('scroll3')
var e=document.getElementById(a.hash.substring(1))
if (e && s3) {s3.scrollTop=e.offsetTop;return false;}
}

and this attribute to each link:

onclick="return sTo(this)"

example:

<a href="#zippy" onclick="return sTo(this)">