JavaScript function in href vs. onClick

Both onclick & href have different behaviors when calling JavaScript directly. Also the script in href won’t get executed if the time difference is short. This is for the time between two clicks.


Here’s an example showing the usage of href vs onClick in JavaScript.

 Live Demo

      <title>JavaScript href vs onClick()</title>
       function myFunc() {
         var v = 0;
         for (var j=0; j<1000; j++) {
      <a href="javascript:myFunc()">href</a>
      <a href="#" onclick="javascript:myFunc()">onclick</a>

Updated on: 03-Jan-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started