Error: jQuery requires a window with a document

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

So everything was working just fine and great until doing npm update and now things do not work quite as they used to.

A little background: in my code I use jquery to parse textual html. I do not use a window and I don't use jsdom. It used to work just fine to do this:

$ = require("jquery");  $(html).find("

").html("The title");

But now I get this: jQuery requires a window with a document

How do I fix this?

回答1:

node.js-jQuery definition in one line:

// definition var $ = require('jquery')(require("jsdom").jsdom().parentWindow);  // usage $("body").append("
TEST
"); console.log($("body").html());


回答2:

The npm package for jQuery definitely used to include jsdom with it, which is why it would work at all in Node: jQuery needs to have some kind of DOM environment to work with.

You can check that old versions used to have it by doing npm install jquery@1.8.3. You'll see jsdom being installed with it. For some reason they seem to have removed jsdom from the recent releases. I don't know why.

However, using jsdom 7.x to run jQuery code is simple:

var jsdom = require("jsdom"); var window = jsdom.jsdom().defaultView;  jsdom.jQueryify(window, "http://code.jquery.com/jquery.js", function () {   var $ = window.$;   $("body").prepend("

The title

"); console.log($("h1").html()); });

The path could be changed to a different version of jQuery or to a local file.

Note: Earlier versions of jsdom, including the 3.x series, would need the line var window = jsdom.jsdom().parentWindow; instead of var window = jsdom.jsdom().defaultView;. 4.x made it so that parentWindow no longer works.



回答3:

I solved it. had to first remove jsdom and jquery then npm install jsdom jquery.

Then this:

var jsdom = require("jsdom");  $ = require("jquery")(jsdom.jsdom().createWindow());  

Turns out that it was important that I had the latest version. Otherwise it didn't work..



回答4:

(Seems all answers are now deprecated)

Here how I made it work (gist using mocha):

var jsdom = require('jsdom'),     window = jsdom.jsdom().defaultView;  jsdom.jQueryify(window, "../js/vendor/jquery.min.js", function(){     var $ = window.$;  }) 


回答5:

I plopped this on the top of my Node.js (server) file that uses jQuery to help render html text ...

var jsdom = require("jsdom").jsdom; jsdom.env({     html : "",     done : function(errs, window) {         global.window = window;     } }); 

and everything comes up roses.



回答6:

You can try the following with these version settings in your package.json:

"jquery": "^2.1.1", "jsdom": "^1.0.0-pre.3" 

Say you have a file called page.htm:

       Document

Html page

left content

Then, you can read from file, create a DOM and window from it with JSDOM and pass it to jquery:

var jsdom = require("jsdom").jsdom; var fs = require('fs'); fs.readFile('page.htm', {encoding: "utf8"}, function (err, markup) {   if (err) throw err;   var doc = jsdom(markup);   var window = doc.parentWindow;   var $ = require('jquery')(window)   var outerLeft = $(".left").clone().wrap('
').parent().html(); var innerLeft = $(".left").html(); console.log(outerLeft, "and ...", innerLeft); });

will output:

left content

and ...

left content



回答7:

This worked for me

var jsdom = require('jsdom'); $ = require('jquery')(new jsdom.JSDOM().window); 


回答8:

You have to provide a window object. To do this, just pass parentWindow from jsdom:

var jsdom = require("jsdom");  var $ = require("jquery")(jsdom.jsdom().parentWindow); 


回答9:

I am currently using this way: check jsdom's documentation

 var html = fs.readFileSync("./your_html_file.html");   jsdom.env({   //url: "http://url_here", //in case you want to load specific url    //html property is used in case of you need to load html string here   html: html,   scripts: ["http://code.jquery.com/jquery.js"],   done: function (err, window) {     var $ = window.$;     //start using jquery now      $("p").each(function(index) {       console.log("paragraph ", index, $(this).text());     });   } }); 


回答10:

I used neoRiley's 2 lines on node command line, and tested jQuery promises on command-line. Added jquery to node command line via npm //https://github.com/UncoolAJ86/node-jquery for instructions.

npm install -S 'jquery@>=2.1' npm install -S 'jsdom@3.1.2' 

/home/sridhar$ node

// make $ available on node commandline >var jsdom = require("jsdom");  >var $ = require("jquery")(jsdom.jsdom().parentWindow); >var promise = new $.Deferred(); >promise.done(function(){console.log('Starting game ..');}); >promise.resolve(); 

promise resolves to success callback, 'Starting game ..' will be printed console output

Starting game.. 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!