On Oct 22, 5:19 pm, DaveC <[EMAIL PROTECTED]> wrote:
> Hi T.J,
>
> I'm not able to use the AJAX.Updater because of policy regarding AJAX
> requests require me to use another ajax. Suffice to say by the time I
> see the response it's two strings one with HTML and one with
> JavaScript.
>
> Here is the path I followed to get where I'm at. The following sample
> code (in my opinion) should correctly register the myfunction() and
> then allow me to call it. This is not the case and I get 'myfunction
> is not defined' in firefox, and the ever unhelpful Object Expected in
> IE.
>
> <head>
> <script src="assets/js/prototype-1.6.0.3.js" type="text/javascript"></
> script>
> <script type="text/javascript">
>
> Event.observe(window, 'load', function() {
>         $('container').update("<p>New HTML</p> <script> alert('this will');
> function myfunction(){ alert('this wont!'); } <\/script>");
>
>         // Fire the new function... does not work?
>         myfunction();
>
> });
>
> </script>
> </head>
> <body>
>   <div id="container"> Old Content.</div>
> </body>
> </html>
>
> Swapping around, as the documentation suggested, to
> var myfunction = function(){ alert('this wont!'); }
> also fails. but here is where it gets interesting, both of the
> following work...
>
> eval("var myfunction2 = function(){ alert('this will 2!'); };" )
> myfunction2();
>
> eval("function myfunction3() { alert('this will 3!'); } " )
> myfunction3();
>
> So I'd have to conclude that prototype does not eval scripts in the
> same way as the eval() function does.?
>
> of course [hitting head on keyboard] that just gave me the solution...
> don't use prototype to inject the js. :)
>
> eval(new_content_js);
> var function_name =  'template_'+ template_name +'_onload';
> eval(function_name + '();' );
>
> Still an interesting exercise and life experience.
>

The problem is that `String.prototype.evalScripts` (which is called by
`Element.update`) executes native `eval` in its *own* scope. `eval`
happens to work in such way that it executes its code within scope of
a caller. In our case, that caller's scope is anonymous function
executed within `evalScripts`. Any function (or variable) declarations
(as in your example) are declared as local ones. In other words they
are lost once anonymous function exits its execution context : )

A workaround is actually pretty simple. If you need to declare a
global variable - define it as a property of global object:

someElement.update('<script type="text/javascript">window.foo =
function(){ return \'foo\' }<\/script>');

// or via `this` from within an anonymous function

someElement.update('<script type="text/javascript>(function()
{ this.foo = function(){ return \'foo\'; }})();<\/script>');

> Thanks
>
> DaveC

[snip]

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to