Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js with browser-based API

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?



Problem courtesy of: ridecar2


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 = {};
// 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.)

Solution courtesy of: Joe White


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js with browser-based API


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription