executing external scripts

985 views
Skip to first unread message

Branden Visser

unread,
Feb 26, 2011, 12:49:24 PM2/26/11
to jsdom
Hi everyone,

Everything I read suggests that jsdom should be executing external
scripts when configured to do so, but if I have jquery included in an
HTML template, it doesn't appear to execute it. Here's a simple
example:

test.js:

jsdom.defaultDocumentFeatures = {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script'],
MutationEvents : '2.0',
QuerySelector : false
};

jsdom.env(template, null, jsdom.defaultDocumentFeatures, function(err,
window) {
writeResponse(response, window);
});

where the 'template' is read from an html file test.html:

<html>
<head>
<title>test</title>
<script type="text/javascript" src="http://code.jquery.com/
jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() { $("body").append('Blah!'); });
</script>
</head>
<body>
Testing
</body>
</html>

When the template is sent in the response it is exactly the content of
test.html, where I am expecting "Blah!" to have been appended to the
body by jsdom (this happens on the client though, obviously). The
result is the same if I use jsdom.jsdom(template) to load the
template.

I understand that I can pass jquery in the scripts array, but I'm
hoping to let the loaded DOM dictate what scripts are available and in
what order they are loaded.

I'm guessing that I've just misinterpreted how jsdom works, but if not
I'll be happy to provide more debugging information if necessary.

Thanks,
Branden

Elijah Insua

unread,
Feb 26, 2011, 1:19:17 PM2/26/11
to js...@googlegroups.com
On Feb 26, 2011, at 12:49 PM, Branden Visser wrote:

Everything I read suggests that jsdom should be executing external
scripts when configured to do so, but if I have jquery included in an
HTML template, it doesn't appear to execute it. Here's a simple
example:

Yeah, this is the default behavior of jsdom.jsdom() and jsdom.html().


jsdom.env(template, null, jsdom.defaultDocumentFeatures, function(err,

This is the problem! The docs state that "Since the web has some absolutely horrible javascript on it, as of jsdom 0.2.0 jsdom.env will not process external resources (scripts, images, etc)."

I understand that I can pass jquery in the scripts array, but I'm
hoping to let the loaded DOM dictate what scripts are available and in
what order they are loaded.

yep, use jsdom.jsdom or jsdom.html

I'm guessing that I've just misinterpreted how jsdom works, but if not
I'll be happy to provide more debugging information if necessary.

I sensed that this might be an reason for contention, but I think its safer for jsdom.env() in jsdom 0.2.0 to simply ignore external resources.  The "easy mode" case needs to be safe until I can find a bullet proof way to ensure they are not going to kill the process/break out of the sandbox.

-- Elijah

Branden Visser

unread,
Feb 26, 2011, 1:30:52 PM2/26/11
to js...@googlegroups.com, Elijah Insua
Thanks for your quick reply Elijah. If you don't mind, I have some
follow-up questions below:

On Sat, Feb 26, 2011 at 1:19 PM, Elijah Insua <tmp...@gmail.com> wrote:
>
> On Feb 26, 2011, at 12:49 PM, Branden Visser wrote:
>
> Everything I read suggests that jsdom should be executing external
> scripts when configured to do so, but if I have jquery included in an
> HTML template, it doesn't appear to execute it. Here's a simple
> example:
>
> Yeah, this is the default behavior of jsdom.jsdom() and jsdom.html().
>
>
> jsdom.env(template, null, jsdom.defaultDocumentFeatures, function(err,
>
> This is the problem! The docs state that "Since the web has some absolutely
> horrible javascript on it, as of jsdom 0.2.0 jsdom.env will not process
> external resources (scripts, images, etc)."
>

Ahh, I had mistakenly interpreted that as *default* behaviour of env,
hence why I felt it necessary to attempt to pass in the jsdom options
that specify external scripts should be executed.

> I understand that I can pass jquery in the scripts array, but I'm
> hoping to let the loaded DOM dictate what scripts are available and in
> what order they are loaded.
>
> yep, use jsdom.jsdom or jsdom.html

I understand the motive to not process external resources by default,
though I didn't have much luck using jsdom.jsdom to process external
scripts with the above example either. Was JQuery supposed to be
executed from the DOM as an external resource if I did something along
the lines of jsdom.jsdom(template)? Do you need me to rework my
example to use jsdom.jsdom for you to see what I'm doing wrong?

Thanks,
Branden

Branden Visser

unread,
Feb 27, 2011, 9:28:10 AM2/27/11
to js...@googlegroups.com, Elijah Insua

Hi elijah, I was digging through the tests and found that I need to use the onload event to grab the resulting template (duh!).

Thanks again for your help!

Branden

On 2011-02-26 1:30 PM, "Branden Visser" <mrvi...@gmail.com> wrote:

Thanks for your quick reply Elijah. If you don't mind, I have some
follow-up questions below:


On Sat, Feb 26, 2011 at 1:19 PM, Elijah Insua <tmp...@gmail.com> wrote:
>

> On Feb 26, 2011, at 12:...

Ahh, I had mistakenly interpreted that as *default* behaviour of env,
hence why I felt it necessary to attempt to pass in the jsdom options
that specify external scripts should be executed.


> I understand that I can pass jquery in the scripts array, but I'm

> hoping to let the loaded DOM ...

Reply all
Reply to author
Forward
0 new messages