473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

troubles with eval and Ajax.Response.responseText in Prototype

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
5 6607
pronerd
392 Expert 256MB
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
16,027 Expert Mod 8TB
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
16,027 Expert Mod 8TB
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
pronerd
392 Expert 256MB
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
16,027 Expert Mod 8TB
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

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

Similar topics

3
by: Jean-Philippe Encausse | last post by:
Hi, I got 2 bugs using AJAX with ISO-8859-1: 1. While serializing form's value using prototype.js I lost accent éŕč ... because it use encodeURIComponent() function. I saw on google, for...
4
by: bobzimuta | last post by:
I'm creating a simple AJAX library. It's an object that will return an array containing the response text or xml. I'm trying to find a way to assign the response as a property of the object, but...
3
by: morganwhitney | last post by:
Hi all, I am developing a web app and I am implementing the JavaScript using the Prototype Framework. I have done all the same stuff from scratch in a previous application and it worked fine, but...
5
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?). I'm using Prototype 1.4 and the my works great...
2
by: hardrock | last post by:
Hello! I'm working with the prototype library version 1.4.0 and having a strange error lately. When I want to make an Ajax.Updater call, it basically works. But as soon as I put the call into...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
1
by: saravj2ee | last post by:
Hi, I have implemented prototype.js file to use AJAX calls in my application. I need to call cross domain to get some value from different server. The HTML and prototype file are hosted in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.