2

What is the reason for using javascript:{} in the code below. Is it similar to href="javascript:" or href="javascript:void(0)"?

<a href="javascript:{}">This is an example</a>
node_modules
  • 4,790
  • 6
  • 21
  • 37
Jim_Joat
  • 71
  • 7

2 Answers2

5

Let hyperlink look like a link but didn't link anything.

<a href="javascript:{}">This is an example</a>

if you remove href attribute, that a tag will be normal text.

D-Shih
  • 44,943
  • 6
  • 31
  • 51
5

It makes a button act like a link, but lets you execute custom JS code instead of linking to a webpage.

For example:

<a href="javascript:{ document.write('hi u just pressed me');}"> Press me pls </a>

acts like

<div onclick="doOnClick()"> Press me pls </a>
<script>
    function doOnClick(){ document.write('hi u just pressed me'); }
</script>

but the browser treats the former like a link.

Drakinite
  • 363
  • 1
  • 13
  • Excellent example, I wasn't thinking about it being inline JS. So, in my example, this "{}" is really a NOP. Any distinction between this example and removing the empty braces? – Jim_Joat Sep 15 '18 at 18:42
  • Very late response, but yes, the "{}" is a NOP. Curly braces can act like code blocks. So in the case of my example, it would have been interpreted as a code block. But I believe that the empty curly braces would be interpreted as the declaration of an empty object. – Drakinite Dec 31 '20 at 04:23