Bug Tracker

Modify

Ticket #4834 (closed enhancement: fixed)

Opened 5 years ago

Last modified 5 years ago

Don't set window.$ until the outro

Reported by: obrie Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc:
Blocking: Blocked by:

Description

$ is the one generic function that's added to the window when loading jQuery that might conflict with other libraries when jQuery is being evaluated. As a result, it's best that $ not be set by jQuery until the outro when the rest of the framework has been evaluated.

This is best shown by example. I have the following test page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <script src="http://www.prototypejs.org/assets/2007/1/18/prototype.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
  </head>
  
  <body></body>
</html>

This has Prototype 1.5.0 and jQuery 1.3.2 embedded on the page. When loaded in Safari 3.2.1, it causes the following error:

Error: NOT_SUPPORTED_ERR: DOM Exception 9  http://www.prototypejs.org/assets/2007/1/19/prototype.js (line 1081)

While this is an older version of Prototype, I think it still demonstrates the issue at hand. What happens is the following:

  1. Prototype loads, defining a global $ function and overriding getElementsByClassName which uses the $ function it has defined.
  1. jQuery loads, overriding the global $ function immediately.
  1. While jQuery is being evaluated (specifically selector.js), it calls getElementsByClassName (overridden by Prototype).
  1. At this point, Prototype's implementation of getElementsByClassName expects to have access to the global $ function it defined. Unfortunately, it's been overridden by jQuery causing the error described above.

Usually you can get away with library conflicts by using jQuery's $.noConflict(true) feature. However, the problem here is that the conflict occurs during the evaluation of jQuery as a result of jQuery adding global functions too early on in the process.

I believe the solution to this admittedly somewhat rare occurrence is to move the declaration of global functions to the bottom of jQuery rather than at the top. This will help solidify jQuery's ability to be compatible with anything else defined on the page and resolve the problem described here.

Attachments

global.diff Download (693 bytes) - added by obrie 5 years ago.
Patch for globals location

Change History

Changed 5 years ago by obrie

Patch for globals location

comment:1 Changed 5 years ago by john

  • Status changed from new to closed
  • Type changed from bug to enhancement
  • Resolution set to fixed

Fixed in SVN rev [6529].

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.