title: How can I inject Javascript (including Prototype.js) in other sites without cluttering the global namespace?
body: <p>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. </p>
  
  <p>We're now working on a piece of code that will get "injected" into other people's sites (picture people adding a &lt;script&gt; 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.</p>
  
  <p>The problem I have is that it's of course not cool to just add a &lt;script&gt; that will include Prototype in people's pages. If we do that in a page that's already using <em>ANY</em> framework, we're guaranteed to screw everything up.<br>
  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 <strong>everything</strong>.</p>
  
  <p>Right now i'm contemplating a number of ugly choices, and I'm not sure what's best...  </p>
  
  <ul>
  <li>Rewrite everything to use jQuery, with a renamed $ object everywhere.</li>
  <li>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.</li>
  <li>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.</li>
  </ul>
  
  <p>I'm wondering what you guys think I could do, and also whether there's some magic option that would solve all my problems...   </p>
  
  <p>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?</p>
  
  <p>I also read once about a technique for bookmarklets, were you add your code like this:</p>
  
  <pre><code>(function() {  /* your code */ })();
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>(function() { 
     /* Full Code of the Prototype file here */
     /* All my code that will run in the "other" site */
  
     InitializeStuff_CreateDOMElements_AttachEventHandlers();
  })();
  </code></pre>
  
  <p>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?</p>
  
  <p>Or is Prototype too complex somehow to isolate it like that?<br>
  (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)</p>