Jim_Joat
Jim_Joat

Reputation: 71

What does href=javascript:{} do?

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>

Upvotes: 2

Views: 1498

Answers (2)

Drakinite
Drakinite

Reputation: 362

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.

Upvotes: 5

D-Shih
D-Shih

Reputation: 46219

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.

Upvotes: 5

Related Questions