How can I inject Javascript (including Prototype.js) in other sites without cluttering the global na

Posted by Daniel Magliola on Stack Overflow See other posts from Stack Overflow or by Daniel Magliola
Published on 2009-08-05T02:56:54Z Indexed on 2010/04/14 0:23 UTC
Read the original article Hit count: 377

I'm currently on a project that is a big site that uses the Prototype library, and there is already a humongous amount of Javascript code.

We're now working on a piece of code that will get "injected" into other people's sites (picture people adding a <script> tag in their sites) which will then run our code and add a bunch of DOM elements and functionality to their site. This will have new pieces of code, and will also reuse a lot of the code that we use on our main site.

The problem I have is that it's of course not cool to just add a <script> that will include Prototype in people's pages. If we do that in a page that's already using ANY framework, we're guaranteed to screw everything up.
jQuery gives us the option to "rename" the $ object, so it could handle this situation decently, except obviously for the fact that we're not using jQuery, so we'd have to migrate everything.

Right now i'm contemplating a number of ugly choices, and I'm not sure what's best...

  • Rewrite everything to use jQuery, with a renamed $ object everywhere.
  • Creating a "new" Prototype library with only the subset we'd be using in "injected" code, and renaming $ to something else. Then again I'd have to adapt the parts of my code that would be shared somehow.
  • Not using a library at all in injected code, to keep it as clean as possible, and rewriting the shared code to use no library at all. This would obviously degenerate into us creating our own frankenstein of a library, which is probably the worst case scenario ever.

I'm wondering what you guys think I could do, and also whether there's some magic option that would solve all my problems...

For example, do you think I could use something like Caja / Cajita to sandbox my own code and isolate it from the rest of the site, and have Prototype inside of there? Or am I completely missing the point with that?

I also read once about a technique for bookmarklets, were you add your code like this:

(function() {  /* your code */ })();

And then your code is all inside your anonymous function and you haven't touched the global namespace at all. Do you think I could make one file containing:

(function() { 
   /* Full Code of the Prototype file here */
   /* All my code that will run in the "other" site */

   InitializeStuff_CreateDOMElements_AttachEventHandlers();
})();

Would that work? Would it accomplish the objective of not cluttering the global namespace, and not killing the functionality on a site that uses jQuery, for example?

Or is Prototype too complex somehow to isolate it like that?
(NOTE: I think I know that that would create closures everywhere and that's slower, but I don't care too much about performance, my code is not doing anything that complex)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about prototype