• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javascript failing to evaluate?

 
Scott McGhee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having some issues getting the response Text of an AJAX request to evaluate the scripts within the HTML response. I am using the Prototype function 'evalScripts()' but it doesn't want to work correctly for some reason. Please see the simple example below 'Main.jsp' which dynamically loads another file 'ajaxtest.jsp' into the innerHTML of a DHTML div tag.

Main.JSPajaxtest.jsp:

Please Help!

The above gives the error back 'myVariable' is undefined (when clicking the 'TEST' button)

Even though clearly the javascript in ajaxtest.jsp is defining it!
[ April 30, 2007: Message edited by: Scott McGhee ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make your life simpler by using Ajax.Updater and setting the evalScripts option to true. Less "moving parts" means easier dubugging.
 
Scott McGhee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - can you check my post again.

I changed it quite a bit: to further simplify I removed any references to combobox javascript.

It is the same issue - but it should be easier to see the problem.
 
Scott McGhee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking through the prototype documentation (http://www.prototypejs.org/api/ajax/updater) I found the following:

About evalScripts and defining functions

If you use evalScripts: true, any <script> block will be evaluated. This does not mean it will get included in the page: they won't. Their content will simply be passed to the native eval() function. There are two consequences to this:

  • The local scope will be that of Prototype's internal processing function. Anything in your script declared with var will be discarded momentarily after evaluation, and at any rate will be invisible to the remainder of the page scripts.
  • If you define functions in there, you need to actually create them, otherwise they won't be accessible to the remainder of the page scripts. ...



  • Which means when I declared the variable 'myVariable' it was not in the global scope and thus could not be evaluated by the function 'alertVariable' when the 'TEST' button is clicked.

    Does anyone know a way around this??
    [ May 01, 2007: Message edited by: Scott McGhee ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic