Why can you execute Javascript code from URL bar of a browser ?

Lately, I have come across a lot of technical write-ups which demonstrate how to perform some operations by typing Javascript code, right in the URL bar. These are implementations of Javascript protocol but most authors have not used the term which makes the title sound ambigous. The use of the term, Javascript protocol can help remove the ambiguity and also help both the author and the reader understand the process better.

Note: This tip is not about “How” to bring up a alert box by using Javascript code in the URL bar, it seeks to explain “Why” the alert box is popped up when called in Javascript code from location bar of browsers.

Ordinarily, when a user type http: the browser is instructed to follow HTTP protocol and go to the URL,following it. As most of us would already have noticed, adding ftp: indicates the browser to open connection using FTP protocol, and it nearly converts the browser to a FTP client.

Similarly, there are many additional protocols which are not used frequently but exist nonetheless, Javascript protocol is one of them. As with HTTP and FTP, when a user enters “javascript: ” the browser interprets the characters after that as Javascript code and executes it.

For example Type the following in the URL bar

javascript:alert(“Hello World”)

The browser interprets text as Javascript code and executes it, as a result you should be able to see a message box with the text “Hello World”

Note: Firefox supports about protocol too, type about:config in the URL location to view all configuration details

150 150 Burnignorance | Where Minds Meet And Sparks Fly!