• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javascript failing to evaluate?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic