简体   繁体   中英

Manipulating jQuery to get an error: "jQuery requires a window and a document"

If anyone flicked through the uncompressed version of jQuery, one may encounter something like:

if ( typeof module === "object" && typeof module.exports === "object" ) {

        // For CommonJS and CommonJS-like environments where a proper `window`
        // is present, execute the factory and get jQuery.
        // For environments that do not have a `window` with a `document`
        // (such as Node.js), expose a factory as module.exports.
        // This accentuates the need for the creation of a real `window`.
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info.
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

Notice the throw new Error() part. If I am not mistaken, it only throws the error if a window and document are not present. I am aware that things like Node.js could trigger this error, but can a normal HTML, CSS and JS web page trigger this in any way?

  1. Node.js will not throw an error here since it contains a global object with a "document" inside it; you can use jquery with Node.js.

  2. You'd be better off using CherrioJS , which is designed specifically to be "like jQuery but for the server.

  3. Attempts to use jQuery with " ElectronJS " or " Headless Chrome " are examples where this error will throw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM