Which JavaScript value should I use for “href”?

If coding is more your thing then the Website Development & Scripting forums are for you. Share server-side scripting and database integration tips, tricks and problems here.
Post Reply
Brianhip
Newbie
Posts: 2
Joined: Tue Jan 07, 2020 10:16 pm

#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: Select all

function myJsFunc() {
    alert("myJsFunc");
}

Code: Select all

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

Code: Select all

function myJsFunc() {
    alert("myJsFunc");
}

Code: Select all

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


User avatar
edwin
Junior Member
Posts: 31
Joined: Thu Sep 28, 2017 6:17 pm

#2

I would honestly suggest neither. I would use a stylized <button></button> for that behavior.

Code: Select all

button.link {
  display: inline-block;
  position: relative;
  background-color: transparent;
  cursor: pointer;
  border: 0;
  padding: 0;
  color: #00f;
  text-decoration: underline;
  font: inherit;
}

Code: Select all

<p>A button that looks like a <button type="button" class="link">link</button>.</p>
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests