Forum: Rails Spinoffs (closed, excessive spam) Using eval on prototype.js

Posted by Carl Youngblood (doncarlosx)
on 2008-06-26 22:34
(Received via mailing list)
Hi,

I'm making an AJAX call to load prototype.js

I then call eval on the request's response text ie

success: function(req){
  eval(req.responseText)
}

This works to load most javascript, but prototype in particular is
confusing me. This error is getting thrown:

Node has no properties

by the following prototype source (line 1526):

if (!window.Node) var Node = { };

if (!Node.ELEMENT_NODE) {

Debug statements immediately before this section show

window.Node != null
Node == null

As far as I understand javascript that should be impossible since
window.Node == Node as long as you're operating at the base level name
space, ie window, not in a function etc.

Obviously none of these shenanigans show up when I load prototype
normally with a script tag. Is it really possible that eval is
behaving differently than the javascript interpreter? Isn't it just a
hook into the javascript interpreter?

Thanks for the help, this is driving me nuts with curiosity!
Carl
Posted by Frederick Polgardy (Guest)
on 2008-06-26 22:38
(Received via mailing list)
Yes, eval() is a hook into the interpreter - BUT, since you're doing 
your
eval() in the context of an Ajax callback, all the outer variable
assignments will define variables local to that function!  So, 
definitely
will not work:

function() {
  eval("var x = 42");
}

x === undefined!

eval("var x = 42");

x === 42

-Fred

On Thu, Jun 26, 2008 at 3:33 PM, dcx <Youngblood.Carl@gmail.com> wrote:

> Obviously none of these shenanigans show up when I load prototype
> normally with a script tag. Is it really possible that eval is
> behaving differently than the javascript interpreter? Isn't it just a
> hook into the javascript interpreter?
>
> Thanks for the help, this is driving me nuts with curiosity!


--
Science answers questions; philosophy questions answers.
Posted by Frederick Polgardy (Guest)
on 2008-06-26 22:38
(Received via mailing list)
Oops: more explicit here:

function f() {
  eval("var x = 42");
}

f();
x === undefined

On Thu, Jun 26, 2008 at 3:37 PM, Frederick Polgardy <fred@polgardy.com>
wrote:

>
>> normally with a script tag. Is it really possible that eval is
>> behaving differently than the javascript interpreter? Isn't it just a
>> hook into the javascript interpreter?
>>
>> Thanks for the help, this is driving me nuts with curiosity!
>
>
> --
> Science answers questions; philosophy questions answers.




--
Science answers questions; philosophy questions answers.
Posted by Carl Youngblood (doncarlosx)
on 2008-06-26 22:55
(Received via mailing list)
Frederick,

I am extremely obliged by your quick and clear response. I feel like
such a noob for not seeing that.

Thanks again!
Carl
This topic is locked and can not be replied to.