Error: jQuery requires a window with a document

后端 未结 12 2271
说谎
说谎 2020-11-29 00:43

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

相关标签:
12条回答
  • 2020-11-29 01:09

    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..
    
    0 讨论(0)
  • 2020-11-29 01:10

    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:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <h1>Html page</h1>
        <div class="left">
            <p>left content</p>
        </div>
    </body>
    </html>
    

    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('<div></div>').parent().html();
      var innerLeft = $(".left").html();
      console.log(outerLeft, "and ...", innerLeft);
    });
    

    will output:

    <div class="left">
    <p>left content</p>
    </div> and ... 
    <p>left content</p>
    
    0 讨论(0)
  • 2020-11-29 01:14

    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());
        });
      }
    });
    
    0 讨论(0)
  • 2020-11-29 01:15

    jsdom seems to have a new version therefore now it works on a slightly different way. Here you have an example:

    const { JSDOM } = require("jsdom");
    const myJSDom = new JSDOM (html);
    const $ = require('jquery')(myJSDom.window);
    

    Now you can search as you used to do on jQuery:

    $("<h1>").html("The title");
    

    Mind the jQuery installation as you should write jquery all lowercase, like npm install jquery.

    0 讨论(0)
  • 2020-11-29 01:16

    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 : "<html><body></body></html>",
        done : function(errs, window) {
            global.window = window;
        }
    });
    

    and everything comes up roses.

    0 讨论(0)
  • 2020-11-29 01:17

    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);
    
    0 讨论(0)
提交回复
热议问题