ala888 Posted July 26, 2014 Share Posted July 26, 2014 I dont fully grasp the concept of why I need to use eval("("+JSON+")"); and not just eval(JSON); Link to comment Share on other sites More sharing options...
davej Posted July 26, 2014 Share Posted July 26, 2014 It would be helpful to see this in context, however it is generally better to use JSON.parse() rather than eval(). http://stackoverflow.com/questions/1843343/json-parse-vs-eval Link to comment Share on other sites More sharing options...
ala888 Posted July 26, 2014 Author Share Posted July 26, 2014 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 More sharing options...
davej Posted July 26, 2014 Share Posted July 26, 2014 It executes the string that you give it. Link to comment Share on other sites More sharing options...
ala888 Posted July 27, 2014 Author Share Posted July 27, 2014 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 More sharing options...
johncc Posted July 27, 2014 Share Posted July 27, 2014 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 More sharing options...
davej Posted July 28, 2014 Share Posted July 28, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now