最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论