Skip to content

Load local copy of jquery? #379

Closed
megablue opened this Issue Dec 24, 2011 · 8 comments

5 participants

@megablue

How do I load a local script file? I tried the code below, however i got nothing beside a null for errors.

jsdom.env("http://www.google.com", [
__dirname + '/scripts/jquery-1.7.1.js'
],
function(errors, window) {
$ = window.$;
console.log(errors);
console.log($().jquery);
});

however the code below worked as long as i am not fetching from a remote site.

jsdom.env("

testing

", [
__dirname + '/scripts/jquery-1.7.1.js'
],
function(errors, window) {
$ = window.$;
console.log(errors);
console.log($().jquery);
});

I found someone faced an exact same problem as mine on http://stackoverflow.com/questions/8480854/jsdom-env-local-jquery-script-doesnt-work

@megablue
megablue commented Jan 7, 2012

so.... any clues?

@rhasson
rhasson commented Jan 8, 2012

I had a similar issue and I found out a resolution like this:

var jquery = fs.readFileSync('public/js/jquery-1.7.1.min.js').toString();

jsdom.env({
html: 'http://somesite.com',
src: [jquery],
done: function (err, window) {...}
});

This reads the external script, in this case jquery, into a variable and converts it to a string. Then in jsdom.env() you load that script using the "src" not "script" config parameter. Then in the "done" callback you should be able to access window.$

I hope this helps,
Roy

@megablue
megablue commented Jan 9, 2012

Thanks. I will try it later on.

@MarcDiethelm

@rhasson Tried it and it's not working for me. Node v0.6.6, jsdom 0.2.10. :(

Edit: It's working. I was seeing a different unrelated error now...

@rhasson
rhasson commented Jan 10, 2012

That's strange. Can you explain what it means "not working" did you try a selector that didn't return anything?
Can you post your code?

If you debug it by putting "debugger;" within your "done" callback function. Once it breaks, type "repl" and then run some jquery commands like $('body').html() or $('div').length and see if you get something. if you get 0 for the length command at least you know jquery is loaded you're just not finding the DOM elements you're looking for.

@MarcDiethelm

My bad. I suspect you didn't see my edit to my post. Your proposed method does in fact work. Sorry about the confusion and thanks a lot for your help!

@megablue

I tried & it works! but i still hope the issue can be solved without using some kind of 'hack'.

@domenic domenic closed this Oct 5, 2012
@gabeno
gabeno commented Feb 12, 2014

I ran into the same problem and (here)[https://gist.github.com/gabeno/8950573] is my solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.