Simulated console with custom commands
What would be the best way to create a list(object,array,whatever) of
virtually hundred of commands that can be run by simply typing them in an
input field?
Currently a user can type anything they want, and the 'console' will tell
them it's unrecognized unless they begin the 'command' with a '/'.
Doing so would call that function and make a change for example, to an
object.
i.e.
"/myname john" would set..
user.name = "john"
"/clear" would set..
$('#console span').remove();
etc..
here's a fiddle to illustrate what I mean..
http://jsfiddle.net/k7sHT/1/
html
<input id="textCommand" type="text"><br/>
js
$('#textCommand').on('keypress', function(e) {
if(e.keyCode==13) {
sendConsole();
}
});
var user = {};
var sendConsole = function() {
value = $('#textCommand').val();
if (value.substring(0,1) === "/") {
alert(value);
} else {
$('body').append("<span>unknown command: "+value+"</span><br />")
$('#textCommand').val("");
}
}
No comments:
Post a Comment