473,508 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

href="#" without scrolling to top

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

Feb 21 '06 #1
7 2989
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/
Feb 21 '06 #2
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

Feb 21 '06 #3
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....vascript%20faq - first hit.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Feb 21 '06 #4
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

Feb 22 '06 #5
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
Feb 23 '06 #6
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/
Feb 23 '06 #7
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
Feb 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
167398
by: Jez | last post by:
Hi, I've created a function which opens a popup window containing a calendar. When a day is clicked, the date is entered into a text box on the parent page and the popup is closed. The link...
10
3706
by: Gernot Frisch | last post by:
Hi, I'm currently writing: <span onclick="window.open(...);">Klick Here</span> but I want to use the <a href> for this, since it is defined in the css script the way I want my link to open....
6
4661
by: kelvlam | last post by:
Hello, I'm a new begininer with JavaScript. I'm trying to figure out which is the best approach, and to understand the differences between them. I have a <Aelement that's suppose to either...
19
2816
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - I have <a href="javascript:somefunction()"what ... ?...
7
2736
by: Veco | last post by:
Hi guys! (and girls if any ;) I want to block my users from submitting a string that includes link(s) stated using a href=... probably there's some function in php? i don't want...
5
8662
by: dangt85 | last post by:
Hello, I have the following page: ... <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.