By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

troubles with eval and Ajax.Response.responseText in Prototype

P: 5
Hi all,

I'm running into a situation where it seems that JS stops executing as soon as I call an eval in my script. I have an Ajax.Request call to a PHP page that builds a JS object and returns it as the responseText. I want to attach that object to the document (or anywhere else that would allow me to access it later), and to do that, I THINK I need to eval it because it's just a string otherwise.

My problem is as soon as I execute a line such as:

eval(e.responseText);

JS seems to halt! I can put an alert immediately after the eval and it will never execute.

I've tried several ways to do this, including putting the whole assignment statement in the PHP response, just the JS object and then assigning a JS var to the result of the eval statement . .

Ex: (PHP output):
document.my_data = {my_prop: 'hello world', my_array: [1,2,2]};
(the above would be evaled like this:)
eval(my_ajax_response.responseText);

(I've also tried this:) (PHP output):
{my_prop: 'hello world', my_array: [1,2,2]}
(the above would be evaled like this:)
document.my_data = eval(my_ajax_response.responseText);

Again, either way, it seems that JS halts execution as soon as I run an eval like this. I can put an alert right after the eval and it is not called. I don't get any JS errors, either!

Am I approaching this horribly wrong? I can get a simple eval example to work:

(PHP output):
alert('hello world');
(the above is evaled like this:)
eval(my_ajax_response.responseText);

And the above works. It just seems like my attempt to assign the result of the eval to something on the page causes problems, but this is exactly what I need to do . . . Any help would be greatly appreciated!
Apr 16 '08 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 392
as soon as I execute a line such as:

eval(e.responseText);

JS seems to halt! I can put an alert immediately after the eval and it will never execute.
With just this line of code there is not much that can be debugged. Are you sure that e object has an accessible attribute named responseText? If so what is the value of that attribute?



then assigning a JS var to the result of the eval statement
I do not think the eval() function returns a result. It just executes the JavaScript passed to it.


Expand|Select|Wrap|Line Numbers
  1.             document.my_data = {my_prop: 'hello world', my_array: [1,2,2]};
  2.  
I am not sure what you are trying to do here, but this does not look at all legal. What is it you want to do? What is the my_data element that you are trying to over write?


I can get a simple eval example to work:

(PHP output):
alert('hello world');
(the above is evaled like this:)
eval(my_ajax_response.responseText);

And the above works.
In that example the alert(); function being run in the eval() function is legal JavaScript code.



It just seems like my attempt to assign the result of the eval to something on the page causes problems,
Correct. I do not see eval() returning anything a result in any of the DOM references.
Apr 16 '08 #2

acoder
Expert Mod 15k+
P: 16,027
I am not sure what you are trying to do here, but this does not look at all legal.
It is legal - see JSON.
Apr 17 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Again, either way, it seems that JS halts execution as soon as I run an eval like this. I can put an alert right after the eval and it is not called. I don't get any JS errors, either!
Instead of an alert, try writing document.my_data.my_prop or document.mydata.my_array[0] to the page.
Apr 17 '08 #4

Expert 100+
P: 392
It is legal - see JSON.
Im still not sure that is true. It really depends on what this my_data element is. If it is a defined element like DIV, SPAN, IMG, etc. I am not sure if you can just over write it like this.

For example if you have the code below. The image element is not over written or removed from the page. There are no errors, but the browser seems to ignore that you are overwriting the element.

[HTML]
<html>
<body>
<img src="http://bytes.com/images/logo4.gif" id="testEl" />
</body>
<script>
var imageElement = document.getElementById('testEl');
imageElement = {my_prop: 'hello world', my_array: [1,2,2]};
</script>
</html>
[/HTML]


Then there is the second issue of trying to store a result from a function call ( eval() ) that does not return a result. We really need to know what it is that is trying to be stored by executing this code.......

Ok, while writing I may have developed an understanding of what is being attempted here. If what is desired is to store a reference to the code being generated so you can reference it again later I think you may want to do something like this.

Expand|Select|Wrap|Line Numbers
  1.     var addMyValues = function() {
  2.         document.my_data.setAttribute('my_prop', 'hello world'); 
  3.         document.my_data.setAttribute('my_array', '[1,2,2]');   
  4.     }
  5.  
  6.     addMyValues();    
  7.  
Apr 17 '08 #5

acoder
Expert Mod 15k+
P: 16,027
It really depends on what this my_data element is. If it is a defined element like DIV, SPAN, IMG, etc. I am not sure if you can just over write it like this.

For example if you have the code below. The image element is not over written or removed from the page. There are no errors, but the browser seems to ignore that you are overwriting the element.

[HTML]
<html>
<body>
<img src="http://bytes.com/images/logo4.gif" id="testEl" />
</body>
<script>
var imageElement = document.getElementById('testEl');
imageElement = {my_prop: 'hello world', my_array: [1,2,2]};
</script>
</html>
[/HTML]
I see what you mean here. Perhaps a simple variable is meant, e.g. window.my_data instead of document.my_data.
Apr 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.