Jump to content

EVAL() JSON


ala888

Recommended Posts

Not all browsers have native JSON support so there will be times where you need to use eval() to the JSON string. Use JSON parser from http://json.org as that handles everything a lot easier for you.Eval() is an evil but against some browsers its a necessary evil but where you can avoid it, do so!!!!!

Does eval() hand me back a string of the evaluated function ? does it plop down pure java script as a return value ?

Link to comment
Share on other sites

how come this does not work then

<html><body><div id = "TEST" style="width:100%;height:100%;"></div><script>var x = eval("function(){document.getElementById('TEST').style.background = 'red';}");x();</script></body></html>
Link to comment
Share on other sites

 

how come this does not work then

<html><body><div id = "TEST" style="width:100%;height:100%;"></div><script>var x = eval("function(){document.getElementById('TEST').style.background = 'red';}");x();</script></body></html>

The TEST DIV needs some content in innerHTML. In your code it has height 0px

 

Not sure why, but the eval parameter without the quotes works as expected.

<!DOCTYPE html><html><body><div id = "TEST" style="width:100%;height:100%;">TEST</div><script>  var x = eval(function(){document.getElementById('TEST').style.background = 'red';});  x();</script></body></html>
Link to comment
Share on other sites

Try...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style></style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><body><div id = "TEST" style="width:100%;height:1000px;"></div><script>var str = "var x = function(){document.getElementById('TEST').style.background = 'red';}";eval(str);x();</script></body></html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...