Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of "javascript:" in code (not URLs)? [duplicate]

Tags:

javascript

I stumbled upon something strange that I never really seen before:

javascript:a=a+10; 

The line above seems to be correct and evaluates happily (at least in Firefox) just like if the javascript: part never existed.

While I do understand the purpose of the old javascript:void(...) style <a href=".."/> used during the dark ages of DHTML, I just can't figure out any useful usage of this prefix in plain JavaScript code.

Does it have some special meaning?

like image 874
sitifensys Avatar asked Sep 02 '13 09:09

sitifensys


People also ask

What is the main use of JavaScript?

JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive. Where HTML and CSS are languages that give structure and style to web pages, JavaScript gives web pages interactive elements that engage a user.

Why is JavaScript special?

JavaScript is a flexible and powerful programming language that is implemented consistently by various web browsers. Along with HTML and CSS, it's a core component of web technology. While HTML is responsible for structure and CSS is responsible for style, JavaScript provides interactivity to web pages in the browser.

What does '$' mean in JavaScript?

$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).

How does JavaScript works on a site?

The way JavaScript works is interesting. Inside a normal Web page you place some JavaScript code (See How Web Pages Work for details on Web pages). When the browser loads the page, the browser has a built-in interpreter that reads the JavaScript code it finds in the page and runs it.


2 Answers

The "javascript:" is a label. It's supposed to be used to identify a loop so that you could then use "break javascript;" to break out of it, but is being misused here. It's harmless, but probably not a good idea to add a label to a statement that isn't a loop.

like image 185
Jules Avatar answered Sep 18 '22 13:09

Jules


It is syntactically valid (it is a label) but useless. It is cargo culting caused by people copy/pasting code without understanding it.

like image 43
Quentin Avatar answered Sep 17 '22 13:09

Quentin