Home  >  Blog, Coding Tips and Notes, JavaScript Favorites  >  Address bar as javascript command line

Address bar as javascript command line

It all started because I was trying to answer a question for someone on Quora. I found a page with the information I was looking for but I had no idea how current it was. The project was listed as ‘outgoing’ and there was no date for when it has first been published. (Which you should never do, please!)

A quick Google search informed me that all I had to do was paste this into the address bar to get my answer:

javascript:alert(document.lastModified)

A little alert pops up with the last modified date.
How cool is that! Your address bar is a command line!

But first a note: In Chrome, if you paste the code it will strip ‘javascript’ at the beginning and search for ‘alert…’ so you’ll need to type ‘javascript’ manually.

Then I got to wondering what else you can possibly do with a javascript command line at your fingertips. (True, Firebug and Chrome’s webmaster tools allow you to run JS, and change whatever you want client-side, but this is cooler.)

Here are a few useful ones:

Change background color
Say there’s a site that’s really tough to read. It has a really light font, or the font color clashes really badly with the background color. Just change the background color and you’re golden!

javascript:document.body.style.backgroundColor="green";void(0);

This code will change it to green. Just replace it with whichever color you prefer.

Check if a website is a spoof
To find out the real URL of a website type this:

javascript:alert("The actual URL is:\t\t" + location.protocol + "//" + location.hostname + "/" + "\nThe address URL is:\t\t" + location.href);

An alert will pop up saying (if you tried it on this page for example):
The actual URL is: http://angeleswebdesign.com
The address URL is: http://angeleswebdesign.com/web-design/javascript-in-address-bar/
In this case the server names in the actual and address URLs match. If they don’t match, the site may be a spoof.

View password behind asterisks
This one’s too long to memorize, but it’s fun. Paste the following code in the address bar and you’ll get an alert with any passwords found on the page.

javascript:(function(){var s,F,j,f,i; s = ""; F = document.forms; for(j=0; j<F.length; ++j) { f = F[j]; for (i=0; i<f.length; ++i) { if (f[i].type.toLowerCase() == "password") s += f[i].value + "\n"; } } if (s) alert("Passwords in forms on this page:\n\n" + s); else alert("There are no passwords in forms on this page.");})();

I can think of two uses for this. You’re typing in a really long password and you want to double check you got it right before hitting enter and having to redo it. Or your browser has your password saved but you want to see what it is.

By the way these work on Android too.

Have fun with it!

Special thanks to this guy for answering my Google quest.

Written by admin