473,503 Members | 1,787 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 12557
yawnmoth wrote on 05 sep 2005 in comp.lang.javascript:
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='doMyClick();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='doMyClick();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='doMyClick();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.javascript 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="doSomething();return false";

Or, you can do this:

onclick="return doSomething()";

Where doSomething will return true or false.

--
Randy
comp.lang.javascript 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.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 6 '05 #9
Donius <ra**************@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.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;visibility:hidden">.</A>
....

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

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
Stephen Chalmers said the following on 9/7/2005 5:26 PM:
Donius <ra**************@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.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;visibility:hidden">.</A>
....

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

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.


You can create the same effect, and have it work in IE, by adding an
Anchor before/after each link.

<a name="thisLink">
<a href="#thisLink">.....</a>

But better is to have JS create links/buttons that are JS dependent.
Then, the non-JS people never know it doesn't work because they never
see it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 7 '05 #11
Randy Webb <Hi************@aol.com> wrote in message news:Hd********************@comcast.com...
Stephen Chalmers said the following on 9/7/2005 5:26 PM:
Donius <ra**************@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.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;visibility:hidden">.</A>
....

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

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.


You can create the same effect, and have it work in IE, by adding an
Anchor before/after each link.

<a name="thisLink">
<a href="#thisLink">.....</a>


It's not quite the same effect, as the page scrolls to position the target anchor at the top of the screen.

You don't need a separate anchor, the link can refer to itself:

<a name='thisLink' href="#thisLink">.....</a>

--
S.C.

Sep 8 '05 #12

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

Similar topics

12
6566
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...
5
4609
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...
3
2630
by: DC | last post by:
Given: a { text-decoration:none; } a:hover { text-decoration:underline; }
1
5723
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...
2
1571
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...
5
10563
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
5116
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...
0
1207
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"><%#...
0
7273
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,...
0
7322
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...
0
5572
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,...
1
5000
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
4667
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
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
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.