18

I get this error and I've managed to narrow it down to:

<a href="javascript:void();" onclick="myFunction();">aaa</a>

That line of code is now the only thing in my source code and still I get the error in title. Any idea why so?

Even when surrounded with the appropriate HTML elements (html, head, body etc) I am still thrown the error. The error shows up in Chrome dev console and via alert if I include a

window.onerror

function in the head tag. It also occurs when the myFunction() method actually exists. As far as I can gather, there is absolutely nothing wrong with that above statement whatsoever.

JJJ
  • 32,902
  • 20
  • 89
  • 102
PsychoMantis
  • 993
  • 2
  • 13
  • 29

3 Answers3

50

Use

<a href="javascript:void(0);" onclick="myFunction();">aaa</a>

void expects a parameter.

There's an interesting discussion on using void(0) or other techniques here.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
keyboardP
  • 68,824
  • 13
  • 156
  • 205
4

Is because void takes one argument. You want:

<a href="javascript:void(0);" onclick="myFunction();">aaa</a>
lorenzo
  • 126
  • 2
2

void is an operator, not a function. It requires a single expression as its operand. () is not a valid expression. The correct syntax is:

<a href="javascript:void 0;" onclick="myFunction();">aaa</a>

You can put parentheses around 0, but they're not necessary, just as you don't need parentheses around 0 when writing 3 + 0.

Barmar
  • 741,623
  • 53
  • 500
  • 612