troubles with eval and Ajax.Response.responseText in Prototype

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wendallsan
    New Member
    • Sep 2006
    • 5

    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.response Text);

    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_dat a = {my_prop: 'hello world', my_array: [1,2,2]};
    (the above would be evaled like this:)
    eval(my_ajax_re sponse.response Text);

    (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_dat a = eval(my_ajax_re sponse.response Text);

    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_re sponse.response Text);

    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!
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    Originally posted by wendallsan
    as soon as I execute a line such as:

    eval(e.response Text);

    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?



    Originally posted by wendallsan
    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.


    Originally posted by wendallsan
    Code:
                document.my_data = {my_prop: 'hello world', my_array: [1,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?


    Originally posted by wendallsan
    I can get a simple eval example to work:

    (PHP output):
    alert('hello world');
    (the above is evaled like this:)
    eval(my_ajax_re sponse.response Text);

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



    Originally posted by wendallsan
    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.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by pronerd
      I am not sure what you are trying to do here, but this does not look at all legal.
      It is legal - see JSON.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by wendallsan
        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_dat a.my_prop or document.mydata .my_array[0] to the page.

        Comment

        • pronerd
          Recognized Expert Contributor
          • Nov 2006
          • 392

          #5
          Originally posted by acoder
          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.getEle mentById('testE l');
          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.

          Code:
              var addMyValues = function() {
                  document.my_data.setAttribute('my_prop', 'hello world'); 
                  document.my_data.setAttribute('my_array', '[1,2,2]');   
              }
          
              addMyValues();

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by pronerd
            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.getEle mentById('testE l');
            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_dat a.

            Comment

            Working...