0
// this is actually defined somewhere else
var strDivHeight = "95%";

// create object, div

var d = document.createElement("DIV");
d.style.height = eval ('"' + strDivHeight + '"');

Works.
But...
eval() is evil, so the saying goes.
Am I also able to get rid of eval() in *this* case?
The suspense grows....:p

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by MattEvans
0

If it were in the form of .95 instead of 95%, you could use parseFloat(string) instead.

The main troubles with eval are a long run time and the possibility of evaluating malicious strings entered by a user.

0

...as long as the string is always a valid CSS property; "95px", "95%" "95pt" etc; you shouldn't need to use eval atall. You're eval-ing putting quotes around the value of a var; so the eval will return an object of type string; the var in that example is already a string, growing suspense... conclusion : the eval does _nothing_. Some kind of property parser gets invoked when you assign a string to a CSS property via script - it will parse the string 95%.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.