Bug Tracker

Opened 7 years ago

Closed 7 years ago

#12207 closed bug (invalid)

jQuery.noConflict deletes jQuery replacement variable...

Reported by: WriterDan Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:

Description

There's an example on the jQuery.noConflict page as follows:

*

Example: Creates a different alias instead of jQuery to use in the rest of the script:

var j = jQuery.noConflict(); Do something with jQuery j("div p").hide(); Do something with another library's $() $("content").style.display = 'none';

*

This method is genius, and I use it quite often. I recently had a problem with it though, because of some code that someone else wrote.

Instead of including:

var j = jQuery.noConflict();

like your example, they used:

var jQuery = jQuery.noConflict();

This seems to work fine in FF, Chrome, Safari, and IE9; however, in IE8 and IE7, it looks like the noConflict directive deletes the new jQuery variable that is being defined by the above statement. That definition was placed outside of any document.ready declarations on this site I was working on, so it looked like jQuery just wasn't loading for IE8 or IE7, which was annoying. At the conclusion of my three-day debug session, I just had to laugh.

The site was using jQuery 1.5, but in amongst my debugging I verified that 1.7.2 behaved the same way.

I don't know if you'd qualify this as a "bug" necessarily or perhaps just poor programming practices on someone else's part, but I had it nailed into my head during graduate school that when building software you shouldn't build it so that it works, you should build it so that it doesn't break. :) Thus, I'm reporting it.

Thanks.

Dan

Change History (1)

comment:1 Changed 7 years ago by dmethvin

Resolution: invalid
Status: newclosed

There's no reason to ever do that. The documentation seems pretty clear on this point.

In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. -- http://api.jquery.com/jQuery.noConflict/

If you say

jQuery.noConflict();

it removes its use of $ but leaves the jQuery object in global scope. No need to declare another variable with the same name.

you should build it so that it doesn't break

Not sure how we'd make a software change inside jQuery to prevent this error...suggestions welcome though.

Note: See TracTickets for help on using tickets.