// 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

Recommended Answers

All 2 Replies

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.

...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%.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.