Welcome to Web Hosting Forum - Net Hosting Talk

Register now and be part of our community! When you register with the Net Hosting Talk you can join in with topics, start new topics, and generally be a part of the first level of our community. It's also quick and totally free, so what are you waiting for?

Which JavaScript value should I use for “href”?

Brianhip

Newbie
Messages
6
Reaction score
1
Hello,

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better in terms of functionality, page load speed, validation purposes, etc.?
Code:
function myJsFunc() {
    alert("myJsFunc");
}

Code:
<a href="#" onclick="myJsFunc();">Run JavaScript Code</a>

or
Code:
function myJsFunc() {
    alert("myJsFunc");
}

Code:
 <a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>
 

devshost

Newbie
Messages
3
Reaction score
0
We usually use the first option because using "#" in href can scroll the page to the top. so by considering this, we use javascript:void(0).
also, the better solution is using buttons because they created for this purpose.
 

Advertisement

Top