"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
HikksNotAtHome wrote on 06 sep 2003 in comp.lang.javascript:I want to turn the string "100,144" to numbers, to use for
resizeTo(100,144) <-- here. Please advice, or post scriptlette.
var myString = "100,144";
var myNumbers = myString.split(',');
myWindow.resizeTo((+myNumbers[0]),(+myNumbers[1]));
or you might concider eval:
var myString = "100,144";
eval("myWindow.resizeTo("+myString+")");
I know, eval is frowned upon,
but it is great executing string stored functionality.
While eval can be used in this way, I would tend to think that if code
seemed to necessitate the use of eval then that is an indicator if bad
design in the code and that an better approach is available for just the
effort of looking.
In this case the potential use of eval is indicated by the fact that two
numbers have been stored in a comma-separated string. If the numbers had
been stored as a two dimensional array eval use would not be indicated
(and the type conversion would also have become unnecessary).
I suspect that the string data is originating in the value property of
an OPTION element, but that just may not be the best approach to the
problem. If the pairs of numbers were stored in an array of
two-dimensional arrays then the selectedIndex property of the select
could be used to index that array and extract the number values (and the
HTML OPTIONs may not need value attributes at all).
The use of eval is almost never (if not actually never) necessary in
JavaScript. On the _very_ rare occasions that eval use is indicated you
need (at minimum) the element of data unknown at run time. A list of
strings representing comma-separated pairs of numbers does not qualify
as unknown at run time.
Richard.