ridecar2
ridecar2

Reputation: 1957

node.js with browser-based API

I have a javascript file that is normally used in a web browser using a script tag. It is a self-executing function that seems to put an object on the window (the window is passed in).

What would be the cleanest way to use it from node.js on the server?

Thanks,

Gareth

Upvotes: 3

Views: 972

Answers (1)

Joe White
Joe White

Reputation: 97818

If all it does is add attributes to window, and you want to get those back out, you can create a global called window:

global.window = {};
require('theLibrary');
// now do something with global.window.theThingItAdded

However, if the library was written for the browser, it's possible that it still won't run because it wants to use the DOM. In that case, you might want to look into jsdom, which aims to give you a spec-compliant DOM inside Node.

(If you're using jsdom, I think that you would use it instead of the global.window bit above -- I think jsdom does that for you, but with a more full-featured window object. I haven't actually used jsdom, though, so I don't know for sure.)

Upvotes: 2

Related Questions