A simple JavaScript executor 

There are 2 notes for this topic, click above title to see all notes.
Joined:
04/09/2007
Posts:
727

January 09, 2008 03:47:31    Last update: August 31, 2008 16:33:28
Many times I need to test some JavaScript snippet before writing code. Maybe I forgot how some JavaScrpit function is supposed to work, maybe I saw some tricky JavaScript code and needed to figure out how it worked. I used MS scripting host for while, but found it cumbersome. Plus, DOM objects are not available in the scripting host.

Below is a simple HTML page I created for this task. You can use the Logger.debug method to output debugging messages.

<html>
<head>
<script language="JavaScript" type="text/javascript">
Logger = {
	buffer : '',
	debug : function(s) {
	    this.buffer += s + '<br/>';
	},
	reset : function() {
	    this.buffer = '';
	},
	flush : function() {
		return this.buffer;
	}
}

function execute() {
    Logger.reset();
	try {
	    result = eval(document.getElementById('src').value);
		if (result) {
		    output(Logger.flush() + result);
	    }
		else {
		    output(Logger.flush());
		}
	}
	catch (e) {
		output(Logger.flush() + e);
	}
}

function output(s) {
	document.getElementById('output').innerHTML = s;
}
</script>
</head>
<body>
Enter script here:<br/>
<textarea id="src" name="src" cols="120" rows="10">
</textarea>
<br/><button id="execute" onclick="execute();">Execute</button>
<p>
Output:
<div id="output"></div>
</p>
</body>
</html>


For example, if you enter
Logger.debug(unescape('http%3a%2f%2fwww.56.com%2fn_v166_%2fc33_%2f17_%2f'));
Logger.debug('abcd'.substr(1));
document.body;

into the text area and click "Execute", you get the following output:
http://www.56.com/n_v166_/c33_/17_/
bcd
[object HTMLBodyElement]
Share |
| Comment  | Tags
 
Easy email testing with http://www.ximailstop.com