Prototype doesn't evaluate JavaScript very well

July 09, 2008 11:33.58 PM

So here's how I call the Ajax.Updater to bring back some HTML with JavaScript in it.

new Ajax.Updater(myDiv, "/fake/test/",
{
parameters: params,
evalScripts: true,
onComplete: function() { doSomething(); };
}
);

Here's the HTML with JavaScript

<script type="text/javascript">
helloWorld = function() { alert( 'Hello World!'); };
</script>

<a href="#" onclick="helloWorld(); return false;">Click me!</a>

Click Me Doesn't Work!!!!! It throws an error saying helloWorld isn't in the scope as a method.

I'm pretty sure it only works when you have actual JavaScript code that has to be evaluated and not based on events. For instance, rather than having the helloWorld() method call come on the onclick handler, it could be listed as a call in the script tags. My rule... don't put JavaScript in HTML response of an AJAX call.

Post a comment

  • So to keep out the annoying people that try to sell Viagra or Porn on my site, please fill out the Captcha so your comments get through. Props to Carbauja since I stole his entire comment interface and back end.

A Django site.