JavaScript eval console
One of the things I frequently find myself needing to do as I explore the JavaScript language is to quickly code up little snippets of JavaScript code and run them. Once I'd tired myself of spinning up little HTML files scattered all over the place, I figured I'd write an interactive console of sorts where I can quickly experiment with JS code. Starting with a simple textarea
embedded in an HTML page I eventually ended up with the page in its current incarnation. Here's a screen shot of the console in action:
The console uses Ajax.org and Mozilla's Ace web editor to get support for things like auto-indent and syntax highlighting. You can hit the keyboard combination Ctrl+Enter
to make the console run the code that is currently in the console. The page automatically loads the latest version of jQuery for you so you have all the $
goodness available to you. There are also a bunch of built-in utility functions available that you can call. Here's a list:
Function | What it does | Example |
---|---|---|
print(msg) |
Prints the string passed to it to the output console. | print("Hello."); |
pprint(msg) |
Prints the string passed to it to the output console using <pre> tag. |
pprint("Pre-formatted hello."); |
sprintf(fmt, ...) |
Similar to the 'C' sprintf function. Returns formatted string. Credit for function goes to http://www.webtoolkit.info/javascript-sprintf.html |
print(sprintf("ms since 1/1/1970: %d", Date.now())); |
escapeHTML |
Use this function if you're trying to print a bit of HTML snippet to the console. If you directly print HTML then the console will simply render it! |
print(escapeHTML("<div>markup</div>")); |
clear |
Clears the message log pane. | clear(); |
If you'd like to access an online version of the console that you can run right away, then use this:
If you'd prefer to use an offline version instead then download it here (note that Google Chrome seems to have problems running this page if served from the disk - so if you're running offline then please stick to IE9, FF or Safari):