File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Javascript failing to evaluate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript failing to evaluate?" Watch "Javascript failing to evaluate?" New topic
Author

Javascript failing to evaluate?

Scott McGhee
Greenhorn

Joined: Apr 10, 2007
Posts: 22
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

You could make your life simpler by using Ajax.Updater and setting the evalScripts option to true. Less "moving parts" means easier dubugging.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Scott McGhee
Greenhorn

Joined: Apr 10, 2007
Posts: 22
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

Joined: Apr 10, 2007
Posts: 22
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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Javascript failing to evaluate?