K
Ken Hilton
Greetings,
I'm mocking up a spreadsheet-like feature for a large legacy application and
I'm using HTML + JavaScript to do it: HTML provides the simple grid and
input fields, etc. and JavaScript handles the "spreadsheet" logic. Since
this is a mockup for early usability tests, I'd really rather not spend time
writing an expression parser/evaluator in JS to allow formula to be input
into spreadsheet cells (which when parsed and evaluated result in the
quantity displayed in the spreadsheet cell.) And, using a 3rd party grid
control is not likely an option. However, some level of such functionality
would be good for our usability study.
My question hence is this: suppose one has an HTML text <INPUT> field
into which a user types a simple, syntactically correct JavaScript
expression, e.g., "result=1+(2*3)". Is there anyway in JavaScript to have
that expression text string evaluated and the value of the JS variable
"result" set accordingly (say "result" is a predefined global JS variable,
if need be)?
Having been off of JavaScript for a couple of years, I was at a loss for
a solution when suddenly I came up w/the "brilliant" idea of dynamically
generating a JavaScript function that wrapped the expression string input by
the user. Then, write this JS function definition string to the document
and simply call it directly to have the expression evaluated. This worked
wonderfully!!!! ... except for the fact that the containing page was
completely replaced by me calling document.write(...) to inject the JS
wrapper function after the page was initially loaded. LOL. Any help you
can lend would be sincerely appreciated.
Ken.
I'm mocking up a spreadsheet-like feature for a large legacy application and
I'm using HTML + JavaScript to do it: HTML provides the simple grid and
input fields, etc. and JavaScript handles the "spreadsheet" logic. Since
this is a mockup for early usability tests, I'd really rather not spend time
writing an expression parser/evaluator in JS to allow formula to be input
into spreadsheet cells (which when parsed and evaluated result in the
quantity displayed in the spreadsheet cell.) And, using a 3rd party grid
control is not likely an option. However, some level of such functionality
would be good for our usability study.
My question hence is this: suppose one has an HTML text <INPUT> field
into which a user types a simple, syntactically correct JavaScript
expression, e.g., "result=1+(2*3)". Is there anyway in JavaScript to have
that expression text string evaluated and the value of the JS variable
"result" set accordingly (say "result" is a predefined global JS variable,
if need be)?
Having been off of JavaScript for a couple of years, I was at a loss for
a solution when suddenly I came up w/the "brilliant" idea of dynamically
generating a JavaScript function that wrapped the expression string input by
the user. Then, write this JS function definition string to the document
and simply call it directly to have the expression evaluated. This worked
wonderfully!!!! ... except for the fact that the containing page was
completely replaced by me calling document.write(...) to inject the JS
wrapper function after the page was initially loaded. LOL. Any help you
can lend would be sincerely appreciated.
Ken.