OpenCraft
Follow OC!
  • Home
  • About
  • Solutions
  • Portfolio
  • Clients
  • Blog
  • Contact

How to use other JavaScript libraries that use "$" in Drupal

Mon, 2007-12-03 00:22
Tags:
  • coding
  • drupal
  • javascript
By: 
Karim Ratib

Drupal uses jQuery for all its JavaScript work. This library defines the variable "$" as a shorthand for jQuery, to allow writing concise code. However, other JavaScript libraries such as script.aculo.us and prototype also use the same variable name, making it a problem to use them in Drupal!

Why would anyone think of using these libraries, since Drupal chose jQuery? Well, the main reason is that some great plugins exist for these libraries, like the LightWindow plugin that I'm trying to integrate within Drupal for a current project.

The solution came from the community, of course. For Drupal 5.x and 6.x, it is a manual solution that requires hand-editing all .js files (including those from contrib modules). Basically, the idea is to wrap each .js file within

(function ($) {
...
})(jQuery);

such that the "$" variable gets bound to jQuery only within the scope of these files, instead of being bound to it globally. Other libraries are then free to use "$" when they need it. I tried it on 5.3 and it worked well as far as I could tell.

NOTE: You don't need to edit jquery.js itself, and of course don't edit the .js plugins made for other JavaScript libraries. To find all .js files that use "$", you can type:

kratib@kryptonite:/var/www/drupal$ find -name '*.js' -exec grep -Hn "<br />$(" "{}" ";"

  • kratib's blog

Our work

PricewaterhouseCoopers

PricewaterhouseCoopers needed a dynamic Web portal to manage the...

Reuters Intranet

Reuters Business Direct team approached OpenCraft to propose an...

View our complete portfolio

Related posts

  • Beware PHP $_REQUEST!
  • Run webinject (nagios plugin) for drupal projects
  • Coloring 1pixelout flash player in audio module
  • Apache MultiViews automatically appends file extensions
  • How to associate default users with projects in 'Case Tracker' module?

More posts by this author

  • Reverse-translating a string back into English in Drupal
  • Who needs Google Spreadsheets: Use Sheetnode!
  • Code contribution: Views Bulk Operations (VBO)
  • The Automatic Resource Destructor pattern
  • Where in the world is open source?

OpenCraft

  • About
  • Team
  • Process
  • Community
  • Media Kit
  • Portfolio
  • Clients
  • Industries
    • Social economic development
    • Multimedia culture
  • Services
    • Drupal development
    • Information architecture
    • Open source software development
    • Social network engineering
  • Request quote
  • Contact
  • Search
Blog

Our profiles at:

Drupal Google
rss

Creative Commons License
search