Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to specify the JavaScript protocol?

Tags:

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?

like image 776
cgp Avatar asked Feb 23 '10 20:02

cgp


People also ask

What is JavaScript protocol?

The iterable protocol allows JavaScript objects to define or customize their iteration behavior, such as what values are looped over in a for...of construct. Some built-in types are built-in iterables with a default iteration behavior, such as Array or Map , while other types (such as Object ) are not.

What is JavaScript pseudo protocol?

Another way that JavaScript code can be included on the client side is in a URL following the javascript: pseudo-protocol specifier. This special protocol type specifies that the body of the URL is arbitrary JavaScript code to be interpreted by the JavaScript interpreter.

What is the purpose of window location protocol property?

Window Location Port port property returns the number of the internet host port (of the current page).

What is pseudo protocol?

pseudoprotocol (plural pseudoprotocols) (computing) Something that can be accessed in the manner of a protocol but is not in fact a protocol. quotations ▼


1 Answers

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:

  • The useless javascript: pseudo-protocol
like image 131
Christian C. Salvadó Avatar answered Oct 13 '22 01:10

Christian C. Salvadó