Search code examples
javascriptcorrectness

When do I need to specify the JavaScript protocol?


I was under the impression that I only need to specify the "protocol" when using JavaScript in URL attributes, such as in hrefs. Is this the only "useful" context for javascript:?

Sensible:

<a href="javascript:alert('Hello')">World!</a>

Silly:

<form onsubmit="javascript:alert('oops!')">

Is this right? Or is there some obscure bug/use case I need to be aware of?


Solution

  • The javascript: pseudo-protocol on event handlers will be only ignored, you don't need it, the JavaScript engine will interpret javascript: as a Label Statement.

    A label simply provides an identifier to a statement, and lets you refer to it elsewhere in your program.

    IMHO, this pseudo-protocol is only useful for bookmarklets...

    Recommended article: