href="#" without scrolling to top

N

Nathan

I've been reading a bit about proper ways to use <a> tags to launch
javascript functions, but I'm still not clear on one issue.

I understand it is common to create a link in HTML such as

<a href="javascriptless.html">

then override the href in java by setting it to href='#', and setting
an onclick function. The problem I'm having is that when I click on the
# link, the browser scrolls to the top of the page. Is there a way to
get around this?

One workaround I found is to use a nonexistant anchor such as
href='#notonthepage'. Both Firefox and IE7 beta do not scroll in this
case. But it seems like a hack. Is there a better way to deal with
this?

Thanks,
Nathan
 
R

Randy Webb

Nathan said the following on 2/20/2006 10:43 PM:
I've been reading a bit about proper ways to use <a> tags to launch
javascript functions, but I'm still not clear on one issue.

I understand it is common to create a link in HTML such as

<a href="javascriptless.html">

then override the href in java by setting it to href='#', and setting
an onclick function. The problem I'm having is that when I click on the
# link, the browser scrolls to the top of the page. Is there a way to
get around this?

<a href="javascriptless.html" onclick="someFunction();return false">

But, if you don't want the behavior of a link, then don't use a link.
Use a button instead:

<button onclick="someFunction()">Execute some JS function</button>
 
N

Nathan

Thanks Randy - just what I was looking for. This must be a FAQ but I
could not find it anywhere by searching google...

Thanks,

Nathan
 
N

Nathan

What I meant was I couldn't find the answer to this question by
searching Google. I hadn't tried looking up the FAQ for the forum
although I should have...

Thanks,

Nathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top