What happens if I combine multiple Javascript libraries on a page?

Take the case, I want to use several javascript libraries on one page, for example, jquery, mootools, scriptalous.

Is that a good idea? Should I do?

+4
source share
4 answers

It depends on the library. Libraries such as YUI or jQuery use a strict namespace system, which means they can work together successfully. For instance. Module template http://www.yuiblog.com/blog/2007/06/12/module-pattern/

However, libraries using a prototype model, such as MooTools or Prototype, are much more complex. They use prototype inheritance to modify their own objects, such as Array, Number, and String.

Although it offers some very elegant solutions and allows library developers to β€œfix” some of their inherent JavaScript problems, this means that they rarely play beautifully together.

In addition, since libraries may seem to be impeccable until a particular medium is used in some obscure way, you can get an intermittent and very complicated way to solve errors.

+3
source

Is that a good idea? Should I do?

No and no.

There are times when you need to use several libraries - for example, when using a lot of legacy code that needs library A, and other parts use library B - but even if plugins like jQuery noConflict can take away some of the pain, make them work together, usually difficult.

There are also massive internal arguments against using multiple DOM processing libraries at the same time because of the way these libraries handle DOM objects domestically. User Binsin once wrote a great post about this, unfortunately, I can’t find it right now.

If you don’t need to use multiple libraries, stick with one.

+2
source

You want to use the noConflict mode. For example: http://api.jquery.com/jQuery.noConflict/

How β€œDo you have to do this?” .. I would say no. Outside of conflicts with multiple frames, longer boot times are implied.

+1
source

What happens if I combine multiple Javascript libraries on a page?

The simple answer is: it depends.

Technically, if each library correctly "namespaces" performs its functions (and namespaces do not collide), does not declare any global functions and does not redefine / supplement any own prototypes of objects, they can live together. In reality, however, it is very likely that this will be a collision (for example, many libraries define their own $() function).

Find the library that best suits your needs and uses it.

+1
source

All Articles