[英]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:如果有人浏览了 jQuery 的未压缩版本,可能会遇到以下情况:
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.注意throw new Error()
部分。 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?我知道像 Node.js 这样的东西可能会触发这个错误,但是普通的 HTML、CSS 和 JS 网页可以以任何方式触发这个错误吗?
Node.js will not throw an error here since it contains a global object with a "document" inside it; Node.js 不会在这里抛出错误,因为它包含一个全局对象,其中包含一个“文档”; you can use jquery with Node.js.你可以在 Node.js 中使用 jquery。
You'd be better off using CherrioJS , which is designed specifically to be "like jQuery but for the server.您最好使用CherrioJS ,它专门设计为“类似于 jQuery,但用于服务器。
Attempts to use jQuery with " ElectronJS " or " Headless Chrome " are examples where this error will throw.尝试将 jQuery 与“ ElectronJS ”或“ Headless Chrome ”一起使用是会抛出此错误的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.