473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript in anchor tags

If using an onclick event handler to execute javascript when an anchor
element is clicked on, what should the href attribute be? #?
javascript:void (0)? Something else?

Sep 5 '05 #1
11 12570
yawnmoth wrote on 05 sep 2005 in comp.lang.javas cript:
If using an onclick event handler to execute javascript when an anchor
element is clicked on, what should the href attribute be? #?
javascript:void (0)? Something else?


Provide a default page for visitors without active js
and give a return false in js:

<a href='/mySorries/sorryNoJS.html' onclick='doMyCl ick();return false;'>
Please click me anyway</a>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 5 '05 #2
Ivo
"yawnmoth" wrote
If using an onclick event handler to execute javascript when an anchor
element is clicked on, what should the href attribute be? #?
javascript:void (0)? Something else?

A normal URL of a normal page, with alternative content for the visitors
without Javascript. By returning false from the event handler, those with
Javascript will never know or care about the href value. Also bots can
follow that link, while they have some problem accessing Javascripted
content.
In case you haven't already, check the newsgroup FAQ as well, especially
<URL: http://www.jibbering.com/faq/#FAQ4_24 >

hth
ivo
Sep 5 '05 #3
*** Evertjan. wrote/escribió (05 Sep 2005 09:11:07 GMT):
Provide a default page for visitors without active js
and give a return false in js:

<a href='/mySorries/sorryNoJS.html' onclick='doMyCl ick();return false;'>
Please click me anyway</a>

Using '#' is particularly annoying. It forces your browser to scroll to top
of page.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Sep 5 '05 #4
Alvaro G Vicario said the following on 9/5/2005 5:45 AM:
*** Evertjan. wrote/escribió (05 Sep 2005 09:11:07 GMT):
Provide a default page for visitors without active js
and give a return false in js:

<a href='/mySorries/sorryNoJS.html' onclick='doMyCl ick();return false;'>
Please click me anyway</a>


Using '#' is particularly annoying. It forces your browser to scroll to top
of page.


Only for authors who don't have the common sense to know how to stop it
from scrolling.

But if all a person wants is a dead link/button to click on to activate
a script, then use a Button and the # problem is gone.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 5 '05 #5

Randy Webb wrote:
<snip>

Only for authors who don't have the common sense to know how to stop it
from scrolling.

But if all a person wants is a dead link/button to click on to activate
a script, then use a Button and the # problem is gone.


That is indeed all I want. I'm using anchor tags to make it so that
clicking on select bits of text will insert something into a textarea.
Sending people to a new page when they're unable to insert something
into a textarea is a bit overkill.

As for alternatives... I want to make something happen when text - not
a button or an image - is clicked.

As for stopping the page from scrolling... I guess I don't have common
sense... Care to enlighten me?

Sep 6 '05 #6
yawnmoth said the following on 9/5/2005 11:58 PM:
Randy Webb wrote:
<snip>

Only for authors who don't have the common sense to know how to stop it
from scrolling.

But if all a person wants is a dead link/button to click on to activate
a script, then use a Button and the # problem is gone.

That is indeed all I want. I'm using anchor tags to make it so that
clicking on select bits of text will insert something into a textarea.
Sending people to a new page when they're unable to insert something
into a textarea is a bit overkill.

As for alternatives... I want to make something happen when text - not
a button or an image - is clicked.

As for stopping the page from scrolling... I guess I don't have common
sense... Care to enlighten me?

You return false in the onclick event handler:

onclick="doSome thing();return false";

Or, you can do this:

onclick="return doSomething()";

Where doSomething will return true or false.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 6 '05 #7
When you set the href of an anchor to '#', it means "go to the current
page, at the top" and so the browser will scroll to the top. When you
return false in javascript, it cancels the click action of the
hyperlink, which means when normally it would follow the link ((this
page, top)), it will not do any hyperlink action ((link-following)), so
there's no scrolling. So for those users without javascript, it
doesn't have a chance to return false ((since none of the script will
run at all)), and will "fall through" and do the hyperlink action. In
a lot of cases, an empty href will do what you're looking for ((that
is, nothing, i think, when there is no javascript available)), or
javascript:void (0), or some similar gimmick.

Hope that was vaguely enlightening. :) It's early where i am though,
so i'll check back later and see if that made any sense.

Sep 6 '05 #8
Donius said the following on 9/6/2005 9:44 AM:
When you set the href of an anchor to '#', it means "go to the current
page, at the top" and so the browser will scroll to the top. When you
return false in javascript, it cancels the click action of the
hyperlink, which means when normally it would follow the link ((this
page, top)), it will not do any hyperlink action ((link-following)), so
there's no scrolling. So for those users without javascript, it
doesn't have a chance to return false ((since none of the script will
run at all)), and will "fall through" and do the hyperlink action. In
a lot of cases, an empty href will do what you're looking for ((that
is, nothing, i think, when there is no javascript available)), or
javascript:void (0), or some similar gimmick.


Setting javascript:void (0) in a non-JS environment doesn't "do nothing".
It navigates. Test it.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 6 '05 #9
Donius <ra************ **@gmail.com> wrote in message news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
So for those users without javascript, it
doesn't have a chance to return false ((since none of the script will
run at all)), and will "fall through" and do the hyperlink action.


In compliant browsers, that situation can be alleviated by setting the href to the name of an anchor placed at the top
of the body element, like:

<BODY>
<A name='screenTop ' style="position :fixed;top:0;vi sibility:hidden ">.</A>
....

Then for relevant links: <A href='#screenTo p' ...

The position of the anchor will always be the current scrolled position, so if the href has to be followed, the page
does not scroll.

--
S.C.
Sep 7 '05 #10

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

Similar topics

12
6586
by: CJM | last post by:
Is it possible to achieve a hover effect for a non-anchor tag in CSS? I can use the following code, but it goes against the grain: <a href="" onclick="return false;">Label</a> Surely there must be a 'proper' way of doing it? Thanks Chris
5
4618
by: TS | last post by:
is there a way to force all links on a page to open in a new browser window without declaring them with a target=_blank attribute? some kind of global thing? I thought i saw it in a stylesheet once, but can't find it now. thanks
3
2642
by: DC | last post by:
Given: a { text-decoration:none; } a:hover { text-decoration:underline; }
1
5729
by: newcreation | last post by:
I call this a weird problem because I can't find anything on it over the internet. Whenever I try to put in an anchor tag within a paragraph, the anchor forces a line break and appears on the next line. I have two sentences in a row with anchor tags, and the anchors appear on the web page one on top of the other. You can see it here: ...
2
1581
by: drabbas | last post by:
http://www.shaneregister.com/listings.asp#15 The following page has anchor tags setup that will link you to the listing. This works fine in firefox. In IE it does not work at all. You can veiw source to see the code. Any help would be appreciated. Thanks
5
10573
by: smita232 | last post by:
HI , i have a code : <a href="#" >Hyperlink</a> Now how do i remove the anchor tag dynamically using Javascript?
8
5126
by: azjudd | last post by:
Thank you in advance for your help on this one. I'm using named anchor tags on a FAQ page with questions listed at the top and answers below; a standard jump-to feature. However, anytime an anchor tag link is used, whether it be a question down to an answer or an answer back to the list of questions, I lose the banner div, a good portion of of...
0
1210
gautamrohit23
by: gautamrohit23 | last post by:
Hello All, i am using repeater to generate the anchor tags <ItemTemplate> <tr> <td><a href="<%#Eval("Word_Link")%>" title="<%# Eval("Word_NAME") %>" target="_blank"><%# DataBinder.Eval(Container.DataItem,"Word_NAME") %></a></td> </tr> </ItemTemplate> i am getting Some anchor tags , but i want to change the font size of the anchor...
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
936
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.