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

html - Understanding "javascript : ... ;" inside <a href=" "> - Stack Overflow

programmeradmin4浏览0评论

What is the meanign of javascript:; that is kept inside the href attribute in an link?

Like

<a href="javascript: ... ;">Link</a>

What is the meanign of javascript:; that is kept inside the href attribute in an link?

Like

<a href="javascript: ... ;">Link</a>
Share Improve this question edited Apr 25, 2012 at 5:16 Starx 79k50 gold badges186 silver badges265 bronze badges asked Apr 25, 2012 at 4:02 pierrotlefoupierrotlefou 40.7k39 gold badges138 silver badges175 bronze badges 1
  • 2 Why so many downvotes? Its not that bad of a question... – Starx Commented Apr 25, 2012 at 5:14
Add a comment  | 

5 Answers 5

Reset to default 9

IF you need to pass a javascript snippet which needs to run instead of the default behavior of an element then you use this javascript: ; syntax.

For example

<a href="javascript:alert('');">Test</a> <!-- Runs on the click of the link -->

Similarly, you can combine these on other events also, like onclick, onchange etc but this is really not necessary, since you can execute the snippet, directly.

The uses of this, i have seen in years are:

  1. <a href="javascript:void(0);">Test</a>
  2. <form action="javascript:void(0);">..</form>

The javascript:; in the href does the same as putting something in the "onclick" property.

Thus,

<a href="javascript:do_something();">Link</a>

is identical to

<a href="#" onclick="do_something();">Link</a>

I'm not sure which is better to use when, but worth mentioning is that you can type these "links" in to the address bar of most modern browsers and it will run.

copy and paste (or type, chrome seems to prohibit this.) in to your address bar

javascript:alert("test");

as well as you can save links with these addresses to bookmarks so that it will run that script on any page you click the bookmark on.

That alone does nothing, but normally javascript: precedes some JavaScript code to indicate that the browser should execute the code instead of treat the href attribute as a URL. Thats it.

This might be intended as a reference that does nothing when clicked and might be accompanied by setting onclick etc. to actually do anything.

upd: While some say that putting a script in href is the same as putting it in onclick, there are some differences, like what happens on right click (and some scripts definitely should not be opened in a new tab or such). Still, my opinion of the practice is that it is somewhat ugly, and not only because of uninformative text in status bar when mouse over the link.

You can imagine that as someone copying that "javascript:;" into the url box and hitting Enter. Since it starts with "javascript:" the browser will know to execute what follows as javascript in the current page. Since all that follows is a ";", nothing will happen, actually. That's the same as clicking a button that has a onClick attribute set to ";".

For example:

<a href="javascript:alert('howdy!')">Click me!</a>

has the same effect that

<button onClick="alert('howdy!')">Click me!</button>

or even

<a href="#" onClick="alert('howdy!')">Click me!</a>

Keep in mind that, since it's a link, most browsers will render its address (in that case "javascript:alert('howdy!')") in the status bar, and that may be undesired (I find it to be particularly ugly).

发布评论

评论列表(0)

  1. 暂无评论