Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it bad practice to use href='javascript:func()' than onclick='func()' for anchors? [duplicate]

Possible Duplicates:
Href for Javascript links: “#” or “javascript:void(0)”?
Why is it bad practice to use links with the javascript: “protocol”?

As question says . .

Which approach is better ?

<a href='javascript:func()' >blah</a>

or

<a href='#' onclick='func()' >blah</a>
like image 837
Arshdeep Avatar asked Nov 10 '10 19:11

Arshdeep


People also ask

Is it bad practice to use onclick?

Adding onclick dynamically The example above works, but is generally considered bad practice. Instead, it's better to separate the content of the page (HTML) from the logic (JS).

Can we call JavaScript function in href?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).

Can we use onclick on link tag?

Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location. Using button tag inside <a> tag: This method create a button inside anchor tag.

Can we pass function to href?

The anwer is: not possible.


2 Answers

Neither.

Use this:

<a href="javascript-disabled-page" onclick="func(); return false;">blah</a>

That way if the user has JS disabled, they will be taken to javascript-disabled-page and the browsing experience isn't ruined.

like image 93
Amy B Avatar answered Sep 22 '22 01:09

Amy B


That depends on what you want to achieve. From a SEO perspective it's better to use links only for actual links, and use click events on other tags for things that doesn't navigate anywhere:

<span class="LinkLookalike" onclick="func();">blah</span>
like image 45
Guffa Avatar answered Sep 19 '22 01:09

Guffa