Skip to content

Instantly share code, notes, and snippets.

@timmywil
Last active December 19, 2015 15:49
Show Gist options
  • Save timmywil/5979255 to your computer and use it in GitHub Desktop.
Save timmywil/5979255 to your computer and use it in GitHub Desktop.
Using jQuery in a CommonJS-like environment and multiple contexts.
// This can be done with what is currently in github (but has not yet been released)
// Using jsdom
var dom = jsdom.jsdom();
var window = dom.createWindow();
var window2 = dom.createWindow();
var factory = require('jquery');
var jquery1 = factory( window );
var jquery2 = factory( window2 );
// Browserify
var factory = require('jquery');
var jQuery = factory();
var anotherjQuery = factory();
@dperini
Copy link

dperini commented Jul 11, 2013

Roughly I need to do this in browsers:

var jQuery = Function( 'global', '(' + factory + ')(global)' )( targetContext );

"factory" is just the string representation of the function that creates a new jQuery instance.

I will just simulate the Node way of achieving it using the module object as transport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment