What does href=javascript:{} do?

Viewed 515

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>
2 Answers

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.

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.

Related