File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript failing to evaluate?" Watch "Javascript failing to evaluate?" New topic

Javascript failing to evaluate?

Scott McGhee

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.


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

Joined: Jan 10, 2002
Posts: 63852

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] [About Bear] [Books by Bear]
Scott McGhee

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

Joined: Apr 10, 2007
Posts: 22
Looking through the prototype documentation ( 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:
    subject: Javascript failing to evaluate?
    It's not a secret anymore!