1

What is the use of 'javascript:methodName(); in an anchor link?

I have seen the following in some code. What is use of the "javascript:"?

 <a href="#" onclick="javascript:getUserName()"/>Click</a>

It works if in both (with and without "javascript:") condition.

4

1 回答 1

4

In this case javascript: is a label:

Provides a statement with an identifier that you can refer to using a break or continue statement.

For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

It is of no use in this case and instead of javascript you could use any other word there.

It is probably only (mistakenly!) used because one typically sees this in the href attribute (where javascript: is a pseudo-scheme):

href="javascript:getUserName()"

but it has a totally different meaning in on* attributes.

Related: Do you ever need to specify 'javascript:' in an onclick?

于 2013-08-12T11:40:51.787 回答