Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- calling & defining functions using funcName() vs. window['funcName']()


Fotiman - 4:03 pm on Jan 7, 2013 (gmt 0)


Let me ask this... are you loading another library such that you need the noConflict? If so, you might consider eliminating that other library (rewriting that code to use jQuery instead), as loading multiple libraries is generally pretty heavy.


I thought declaring it outside makes it global, and therefore available in the function

It does. But when noConflict is run, your global $ will be replaced. This is another reason why having your own namespace would be helpful.

Your scenerio sounds like the following:
1. $ is defined by some other library as something other than jQuery
2. jQuery is loaded and $ is set to jQuery
3. noConflict resets $ back to previous definition
4. Your global functions use $, assuming that $ == jQuery
5. ready() executes and gives a $ == jQuery within it's callback function

I can't think of a good way around this problem, but here are a few options.

1. Create a global variable "$$" and set that to jQuery, then replace all instances of $ in your scripts with $$ (fairly easy to do with any text editor). Alternatively, replace every instance of $ with "jQuery".

2. Modify your functions to take a jQuery object as a parameter:
function funcName($) {...}
Then when you call those functions from ready(), just pass in $


Thread source:: http://www.webmasterworld.com/javascript/4533285.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com