href="#" without scrolling to top

  • Follow


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

0
Reply nathan.funk (17) 2/21/2006 3:43:46 AM

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>

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
0
Reply Randy 2/21/2006 3:48:23 AM


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

0
Reply Nathan 2/21/2006 3:55:27 AM

Nathan wrote:

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

http://www.google.com/search?q=comp.lang.javascript%20faq - first hit.

-- 
David Dorward       <http://blog.dorward.me.uk/>   <http://dorward.me.uk/>
                     Home is where the ~/.bashrc is
0
Reply David 2/21/2006 7:52:45 AM

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

0
Reply Nathan 2/22/2006 11:32:10 PM

Nathan wrote:
> What I meant was I couldn't find the answer to this question by
> searching Google.

FYI, it's also covered quite well in my Javascript Best Practices document 
at
http://www.JavascriptToolbox.com/bestpractices/

-- 
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


0
Reply Matt 2/23/2006 3:50:54 AM

Matt Kruse said the following on 2/22/2006 10:50 PM:
> Nathan wrote:
>> What I meant was I couldn't find the answer to this question by
>> searching Google.
> 
> FYI, it's also covered quite well in my Javascript Best Practices document 
> at
> http://www.JavascriptToolbox.com/bestpractices/
> 

And that URL is in just about every post I make :)
Anything coming as far as a domain name for that or will it stay a 
sub-folder?

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
0
Reply Randy 2/23/2006 7:56:58 AM

Randy Webb wrote:
>> http://www.JavascriptToolbox.com/bestpractices/
> Anything coming as far as a domain name for that or will it stay a
> sub-folder?

I suppose I could create http://www.JavascriptBestPractices.com/ and 
redirect to the above url, but that seems like over-kill to me. I already 
have too many domain names ;)

-- 
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


0
Reply Matt 2/23/2006 7:38:28 PM

7 Replies
211 Views

(page loaded in 0.109 seconds)

Similiar Articles:













7/26/2012 9:08:44 AM


Reply: