getting external resources to execute

203 views
Skip to first unread message

noah grant.

unread,
Feb 20, 2013, 12:53:55 PM2/20/13
to jsdom
Hello,

I'm not having much success getting scripts to execute using
jsdom.jsdom(). I seem to have followed all of the docs, and gone
through a lot of the code. I see how setting

defaultDocumentFeatures = {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script'],
etc etc
}

and calling jsdom.jsdom(data).createWindow() sets a property
_implementation._features on document, but the value on each property
in the _features object is just ['script']. None of my scripts are
actually being executed, and the HTML that I'm retrieving from a test
page via node.io is without DOM-injected elements onload.

How can I get the scripts to execute?

/* node.io's getHtml fcn, working correctly */
this.getHtml('http://www.ispyfoolprooffour.com', function(err, $,
data) {

// can we call/load scripts here??
window = jsdom.jsdom(data, null, {
feautres: {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script']
}
}).createWindow();


res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write(window.document.body.innerHTML);
res.end();
self.emit(); // irrelevant at the moment
});

the innerHTML should contain <li> elements inside the ul#post element.

I've also tried just calling jsdom.jsdom(data).createWindow(), using
node.io's options parameter to pass the documentFeatures
configuration. Neither works, and I'm inclined to use Phantomjs
instead, which does return the right HTML.

Please advise! Thanks so much!

Noah

Domenic Denicola

unread,
Feb 20, 2013, 11:58:08 PM2/20/13
to js...@googlegroups.com
It should be working fine. Can you show us a small reproducible test case where they don't load, not involving a third-party library or website outside of our control? E.g. can you reproduce this bug using just a simple page as an HTML string, with external <script> resources that are well-known libraries and/or simple code hosted in a gist, instead of that URL loaded using this "node.io" thing?
> --
> You received this message because you are subscribed to the Google Groups
> "jsdom" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jsdom+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

noah grant.

unread,
Feb 21, 2013, 12:33:44 PM2/21/13
to jsdom
You mean something like this?

https://gist.github.com/noahgrant/7de2272b132a8de936e9

the response from the innerHTML call gives the script literals, it
doesn't execute (ie create a div element with "hello world" in the
body). How can I get the scripts to execute and return the injected
DOM? How can I call global js functions from these scripts, if
necessary?

Thanks,

Noah

On Feb 20, 8:58 pm, Domenic Denicola <dome...@domenicdenicola.com>
wrote:

Elijah Insua

unread,
Feb 21, 2013, 12:50:48 PM2/21/13
to js...@googlegroups.com
Noah,

What version of jsdom are you running?  The latest appears to work fine: https://gist.github.com/tmpvar/5006642

jsdom.jsdom will fetch/execute js automatically, so there is no need for "features" (which was misspelled btw).

When using jsdom.jsdom a good thing to keep in mind is that it's going to take some time before the scripts have loaded so you need to wait for document.onload and/or window.onload depending on the page.

-- Elijah

noah grant.

unread,
Feb 21, 2013, 1:31:20 PM2/21/13
to jsdom
Thanks, Elijah.

I'm on 0.5.0. I thought the onload callback would fix my issue, but
i'm still not getting back the right html--my example in another gist,
including the node.io fcn "getHtml", which provides us with the
parameter "data" (the page HTML). The response leaves the ul#posts
element empty, and after ispy_concat.min.js is run, that element
should contain three <li>'s. It just looks like ispy_concat.min.js is
not getting run:

https://gist.github.com/noahgrant/5006864

Thanks so much!

Noah

On Feb 21, 9:50 am, Elijah Insua <tmp...@gmail.com> wrote:
> Noah,
>
> What version of jsdom are you running?  The latest appears to work fine:https://gist.github.com/tmpvar/5006642
>
> jsdom.jsdom will fetch/execute js automatically, so there is no need for
> "features" (which was misspelled btw).
>
> When using jsdom.jsdom a good thing to keep in mind is that it's going to
> take some time before the scripts have loaded so you need to wait for
> document.onload and/or window.onload depending on the page.
>
> -- Elijah
>

Elijah Insua

unread,
Feb 21, 2013, 1:43:29 PM2/21/13
to js...@googlegroups.com
Looks like `this` inside of the getHtml callback is a jsdom window so there is no need to create another window.

I'm not positive, but maybe the XMLHttpRequests under the hood of $.ajax are not handled?

noah grant.

unread,
Feb 21, 2013, 1:57:58 PM2/21/13
to jsdom
Hmm. As in no XHR support in node.io? Isn't node.io only just
supplying the initial html data as a parameter?

Well, I did a quick test, inserting all the html from that page as a
string literal and eliminating node.io (basically just using your
previous gist), and the html output was the same--no <li> elements in
ul#posts. So I'm not sure...

Noah

On Feb 21, 10:43 am, Elijah Insua <tmp...@gmail.com> wrote:
> Looks like `this` inside of the getHtml callback is a jsdom window so there
> is no need to create another window.
>
> I'm not positive, but maybe the XMLHttpRequests under the hood of $.ajax
> are not handled?
>

noah grant.

unread,
Feb 26, 2013, 8:42:51 PM2/26/13
to jsdom
Hi Elijah/Domenic,

This might be a better example:

https://gist.github.com/ngfluid/5044155#file-gistfile1-txt

I should get back a late-loaded image to the browser: '/us/images/
washers/wt6001hva/wt6001hva-200x150.jpg'

but instead I still get the loader image that ends in 'b.gif'

$.ajax is supported in this script (check)
$ is bound to the DOM (check)
I still can't seem to pull in and execute external scripts nor scripts
that come with the page, such as the lg.min.js on this page, which
launches the lg.init() fcn, which calls the loadImages() fcn.  I
believe dependencies are managed by head.js.

Any ideas to help achieve this?

Thanks so much!

Noah

noah grant.

unread,
Feb 27, 2013, 7:52:33 PM2/27/13
to jsdom
Just to add to what I'm trying to do, upon further inspection it looks
like only scripts with absolute paths are being loaded and executed,
but nothing else is.

Thanks!

Noah
Reply all
Reply to author
Forward
0 new messages