[node]jQuery requires a window with a document

在node中服务器端使用jquery遇到jQuery requires a window with a document

使用jquery版本为v3.1.1,nodejs版本为4.0.3

原因在于jquery的运行需要window的运行环境,在浏览器端是没问题的,而在服务器上就不包含了,需要jsdom创建一个window给它.
jquery 文档解释如下:

// 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.

解决方法就是用jsdom 创建一个类似浏览器的窗口对象。

首先安装jsdom

npm install jsdom

然后使用它:

var jsdom = require(“jsdom”);
var $ = require(“jquery”)(jsdom.jsdom().defaultView);

//下面就可以使用$了。

需注意的是,由于jsdom版本更新问题,(我用的是9.8.3版本)jsdom().defaultView 这个写法在一些旧版本可能无效,比如之前用的是jsdom().createWindow()。stackoverflow有过讨论,详见jsdom.jsdom missing a “createWindow” method

当然,最好的办法是看jsdom文档

/*! * jQuery JavaScript Library v1.12.4 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2016-05-20T17:17Z */ (function( global, factory ) { 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 ); } // Pass this if window is not defined yet }(typeof window !== "undefined" ? window : this, function( window, noGlobal ) { // Support: Firefox 18+ // Can't be in strict mode, several libs including ASP.NET trace // the stack via arguments.caller.callee and Firefox dies if // you try to trace through "use strict" call chains. (#13335) //"use strict"; var deletedIds = []; var document = window.document; var slice = deletedIds.slice; var concat = deletedIds.concat; var push = deletedIds.push; var indexOf = deletedIds.indexOf; var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty; var support = {}; var version = "1.12.4", // Define a local copy of jQuery jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery.fn.init( selector, context ); }
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值