Terry Li
Terry Li

Reputation: 17268

JavaScript library: jQuery vs. Prototype

I just started my very first JavaScript tutorial couple of days ago. I've come to the point where they explain the use of libraries. Two questions in mind thus far:

  1. getElementById is implemented as $("#id") in jQuery and as $("id") in Prototype. Why the inconsistency?

  2. If both libraries are included, how can the browser decide which object should be returned for $("")?

Upvotes: 0

Views: 381

Answers (3)

p.s.w.g
p.s.w.g

Reputation: 149020

1. Why the inconsistency?

The two libraries were implemented at different times, by different teams, and do very different things. Why would you expect consistency?

2. If both libraries are included, how can the browser decide which object should be returned for $("")?

That depends on exactly how you include them. However, jQuery has facilities to avoid conflicts like this, see jQuery.noConflict. It's been some time since I've used prototype, but it may have similar functionality, as well.

Upvotes: 1

suren
suren

Reputation: 981

This link will help you to understand handling conflicts while using jquery with other library like prototype http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

Upvotes: 0

user229044
user229044

Reputation: 239301

The browser doesn't "decide", whichever one you include second overwrites the first.

In situations where two libraries are fighting over $, use jQuery instead:

jQuery('#id') // same as $('#id') when $ == jQuery

You can then return control over $ to Prototype with jQuery.noConflict.

As for the inconsistency, they're different libraries written at different times by different people, with no intention of making them compatible. Consistency was never a consideration.

Upvotes: 1

Related Questions