What anchor < a > attribute should hold the url that will be followed when the hyperlink is clicked?

I want to run a simple JavaScript function on a click without any redirection.

Is there any difference or benefit between putting the JavaScript call in the href attribute (like this):

....

vs. putting it in the onclick attribute (binding it to the onclick event)?

saw

3262 gold badges4 silver badges12 bronze badges

asked Jul 1, 2009 at 18:59

SkunkSpinnerSkunkSpinner

11.2k7 gold badges40 silver badges52 bronze badges

2

bad:

link text

good:

link text

better:

link text

even better 1:

link text

even better 2:

link text

Why better? because return false will prevent browser from following the link

best:

Use jQuery or other similar framework to attach onclick handler by element's ID.

$('#myLink').click(function(){ MyFunction(); return false; });

Gray

113k22 gold badges283 silver badges349 bronze badges

answered Jul 5, 2012 at 16:13

25

Putting the onclick within the href would offend those who believe strongly in separation of content from behavior/action. The argument is that your html content should remain focused solely on content, not on presentation or behavior.

The typical path these days is to use a javascript library (eg. jquery) and create an event handler using that library. It would look something like:

$('a').click( function(e) {e.preventDefault(); /*your_code_here;*/ return false; } );

wilbbe01

1,9211 gold badge23 silver badges38 bronze badges

answered Jul 1, 2009 at 19:05

ParandParand

99.6k45 gold badges151 silver badges184 bronze badges

8

In terms of javascript, one difference is that the this keyword in the onclick handler will refer to the DOM element whose onclick attribute it is (in this case the element), whereas this in the href attribute will refer to the window object.

In terms of presentation, if an href attribute is absent from a link (i.e. ) then, by default, browsers will display the text cursor (and not the often-desired pointer cursor) since it is treating the as an anchor, and not a link.

In terms of behavior, when specifying an action by navigation via href, the browser will typically support opening that href in a separate window using either a shortcut or context menu. This is not possible when specifying an action only via onclick.


However, if you're asking what is the best way to get dynamic action from the click of a DOM object, then attaching an event using javascript separate from the content of the document is the best way to go. You could do this in a number of ways. A common way is to use a javascript library like jQuery to bind an event:

link text

answered Jul 1, 2009 at 19:54

AdamAdam

1,7241 gold badge14 silver badges33 bronze badges

1

EDITOR WARNING: See the comments, the use of 'nohref' is incorrect in this answer.

I use

Click HERE

A long way around but it gets the job done. use an A style to simplify then it becomes:

HERE

Cameron

2,7331 gold badge29 silver badges30 bronze badges

answered Feb 29, 2012 at 3:01

4

The top answer is a very bad practice, one should never ever link to an empty hash as it can create problems down the road.

Best is to bind an event handler to the element as numerous other people have stated, however, do stuff works perfectly in every modern browser, and I use it extensively when rendering templates to avoid having to rebind for each instance. In some cases, this approach offers better performance. YMMV

Another interesting tid-bit....

onclick & href have different behaviors when calling javascript directly.

onclick will pass this context correctly, whereas href won't, or in other words no context won't work, whereas no context will.

Yes, I omitted the href. While that doesn't follow the spec, it will work in all browsers, although, ideally it should include a href="javascript:void(0);" for good measure

answered Mar 11, 2017 at 8:01

slothstronautslothstronaut

8121 gold badge12 silver badges14 bronze badges

1

the best way to do this is with:

The problem is that this WILL add a hash (#) to the end of the page's URL in the browser, thus requiring the user to click the back button twice to go to the page before yours. Considering this, you need to add some code to stop event propagation. Most javascript toolkits will already have a function for this. For example, the dojo toolkit uses

dojo.stopEvent(event);

to do so.

answered Jul 2, 2009 at 2:29

linusthe3rdlinusthe3rd

3,3353 gold badges27 silver badges42 bronze badges

3

In addition to all here, the href is shown on browser's status bar, and onclick not. I think it's not user friendly to show javascript code there.

answered Jul 1, 2009 at 19:09

KamareyKamarey

10.6k7 gold badges56 silver badges69 bronze badges

This works

Click Here

answered Feb 8, 2018 at 8:19

What anchor < a > attribute should hold the url that will be followed when the hyperlink is clicked?

TitusMixTitusMix

1012 silver badges9 bronze badges

1

Having javascript: in any attribute that isn't specifically for scripting is an outdated method of HTML. While technically it works, you're still assigning javascript properties to a non-script attribute, which isn't good practice. It can even fail on old browsers, or even some modern ones (a googled forum post seemd to indicate that Opera does not like 'javascript:' urls).

A better practice would be the second way, to put your javascript into the onclick attribute, which is ignored if no scripting functionality is available. Place a valid URL in the href field (commonly '#') for fallback for those who do not have javascript.

answered Jul 1, 2009 at 19:10

What anchor < a > attribute should hold the url that will be followed when the hyperlink is clicked?

zombatzombat

91k24 gold badges155 silver badges164 bronze badges

2

it worked for me using this line of code:

text

answered Dec 10, 2014 at 15:14

What anchor < a > attribute should hold the url that will be followed when the hyperlink is clicked?

First, having the url in href is best because it allows users to copy links, open in another tab, etc.

In some cases (e.g. sites with frequent HTML changes) it is not practical to bind links every time there is an update.

Typical Bind Method

Normal link:

Google

And something like this for JS:

$("a").click(function (e) { e.preventDefault(); var href = $(this).attr("href"); window.open(href); return false; });

The benefits of this method are clean separation of markup and behavior and doesn't have to repeat the function calls in every link.

No Bind Method

If you don't want to bind every time, however, you can use onclick and pass in the element and event, e.g.:

Google

And this for JS:

function Handler(self, e) { e.preventDefault(); var href = $(self).attr("href"); window.open(href); return false; }

The benefit to this method is that you can load in new links (e.g. via AJAX) whenever you want without having to worry about binding every time.

answered Mar 2, 2019 at 21:05

jchavannesjchavannes

2,2001 gold badge24 silver badges13 bronze badges

Personally, I find putting javascript calls in the HREF tag annoying. I usually don't really pay attention to whether or not something is a javascript link or not, and often times want to open things in a new window. When I try doing this with one of these types of links, I get a blank page with nothing on it and javascript in my location bar. However, this is sidestepped a bit by using an onlick.

answered Jul 1, 2009 at 19:08

PeterPeter

4212 silver badges8 bronze badges

The most upvoted answer is obsolete today

I would recommend the exact opposite, see step by step with reasons:

good:

link text

It depends, might be good, because crawlers follows href targets and if there is any meaningful content produced by MyFunction() (dynamic link), it is followed more likely than in the click event, which may have multiple or none listeners.

bad:

link text

# means meaningless link, crawlers are often interested only in first x links, so it can prevent them to follow some meaningful links later in the page.

worse:

link text

Same as previous plus return false prevents following the link. If some other scripts want to add another listener and update the target (say to redirect via proxy), they can't without modifying the onclick (okay, it's just a minor setback as such use cases are rather theoretical).

worst:

Use jQuery or other similar framework to attach onclick handler by element's ID.

$('#myLink').click(function(){ MyFunction(); return false; });

jQuery is outdated in 2020+ and should not be used in new projects.

Events in href

The href attribute handler doesn't get the event object, so the handler doesn't implicitly see which link was the source. You can add it in onclick handler, which fires before the href is followed:

JS based link

answered Nov 23, 2021 at 8:58

Jan TuroňJan Turoň

29.7k21 gold badges117 silver badges163 bronze badges

One more thing that I noticed when using "href" with javascript:

The script in "href" attribute won't be executed if the time difference between 2 clicks was quite short.

For example, try to run following example and double click (fast!) on each link. The first link will be executed only once. The second link will be executed twice.

href onclick

Reproduced in Chrome (double click) and IE11 (with triple click). In Chrome if you click fast enough you can make 10 clicks and have only 1 function execution.

Firefox works ok.

answered Dec 15, 2016 at 9:59

kolobokkolobok

3,4593 gold badges34 silver badges53 bronze badges


answered Dec 7, 2017 at 17:03

I experienced that the javascript: hrefs did not work when the page was embedded in Outlook's webpage feature where a mail folder is set to instead show an url

answered Dec 16, 2018 at 6:40

ChristianChristian

2,8124 gold badges31 silver badges33 bronze badges

click here

answered Mar 24, 2021 at 12:11

What anchor < a > attribute should hold the url that will be followed when the hyperlink is clicked?

1

This works as well

Click Here

(onclick) did not work for me in an Angular project with bootstrap.

answered May 21, 2021 at 8:59

What is URL anchor?

The anchor text is also known as the link label or link title. The words contained in the anchor text help determine the ranking that the page will receive by search engines such as Google or Yahoo and Bing. Links without anchor text commonly happen on the web and are called naked URLs, or URL anchor texts.