3

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

4
  • Are you looking to: (a) use this file from Node without making any changes? (b) make a separate version of it that will work in Node? or (c) make a single version of the file that's friendly to both browser and Node? Commented Feb 7, 2012 at 14:08
  • It would need to be the same file, sorry for the delay. Commented Feb 14, 2012 at 12:37
  • Okay, that eliminates one of the three choices. Are you willing/able to make modifications to this file to make it work (c), or do you have to use the original file as-is (a)? Commented Feb 14, 2012 at 12:51
  • Sorry, I meant same file, no changes. Commented Feb 14, 2012 at 12:53

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.