0

Can someone please explain the difference between:

onclick="javascript:fnName(this);"

...and...

onclick="fnName(this);"

Is there any performance hit? or when to use what?

raina77ow
  • 103,633
  • 15
  • 192
  • 229
Saurabh Mahajan
  • 2,937
  • 7
  • 25
  • 32

2 Answers2

0

Not actually, and usually you don't write the first one. Because onclick handler is handled by the JavaScript, so calling JavaScript to handle it won't be a good bet.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
0

In the context of event attributes it's completely useless. It's mainly used inside of the href attribute and allows you to create a pseudo-onclick event:

<a href="javascript:alert('hello')">click me</a>

Also you can just put it in the location input in your browser and run scripts. It's simillar to the console input.

Edit: I realized it's not really useless but it has completely different usage than you would thought. In the context of these event attributes it behaves like in a normal code and so it's a label. Try this:

<div onclick="javascript:while(true){while(true){alert('hello');break javascript;}}">click me</div>
Razem
  • 1,421
  • 11
  • 14