JScript location bar silliness

So, you know how you can go to your browser's location bar and type a bit of JScript code to have it execute? Go ahead, try it now, just type this and hit Enter:

javascript:alert('hi there')

This evaluates a bit of code, but you can also use this evaluation to create an anonymous function and execute it.

javascript:(function(){var i,s=""; for (var i in document.images) s += i + ":" + document.images[i].src + "\r\n"; alert(s)})()

Note that you have to surround the function in parens (highlighted in blue in the original), otherwise it looks like a top-level function declaration and then the invocation after that doesn't make sense, so nothing happens.

Personally, I just type this bit by bit, just to make sure everything lines up.

javascript:(function(){alert("do something here");})()

Of course there are better options if you're going to be doing anything that you don't want to lose just by tab'bing away and back (I imagine the browser thinks you never finished typing your new destination and shows you the real URL for the page you're seeing). Just go ahead and press F12 to bring up the Developer Tools window in IE8, go to the Scripts tab and type away on the right-hand side part of the window. That gives you multiline, history of commands, and the result of evaluation - a much nicer story.


Comments (0)

Skip to main content