html - What does href=javascript:{} do? - Stack Overflow

What is the reason for using javascript:{} in the code below. Is it similar to href="javascript:&q

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>

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>
Share Improve this question edited Sep 15, 2018 at 18:16 node_modules 4,9356 gold badges25 silver badges38 bronze badges asked Sep 15, 2018 at 18:11 Jim_JoatJim_Joat 717 bronze badges 2
  • Sort of - yes, it's the same as those. It disables the link. – VLAZ Commented Sep 15, 2018 at 18:14
  • My friend your answer is here: stackoverflow./a/7755207/10325004 – Akshay Jain Commented Sep 15, 2018 at 18:21
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745401639a4626131.html

相关推荐

  • html - What does href=javascript:{} do? - Stack Overflow

    What is the reason for using javascript:{} in the code below. Is it similar to href="javascript:&q

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信