mootools: $ not defined - javascript

I've strange error i don't understand. I'm just moving my code from jQuery over to Mootools because it's much cleaner than the jQuery mess.
Now when i use the
$$('div.canvas')
in mootools i get the correct element.
When i try
$(document).getElement('div.canvas')
it tells me that $ is not defined. How can $$ and all helper functions like $lambda etc. be defined but not $?
Has something changed there from 1.1 to 1.2 and the docs are not updated yet?

as someone pointed out, when $ is defined, mootools 1.2.3+ will not take it over, it will revert to using document.id instead. this did not used to happen before that release so it largely depends on the version you are referencing. but it's certainly changed since 1.11 and it IS documented, read the announcement here http://mootools.net/blog/2009/06/22/the-dollar-safe-mode/
to your application design this means that, if your structure is...
load jquery (no need for noconflict, does not matter)
load mootools
... it can work as follows:
$("#foo"); // jquery
document.id("foo"); // mootools
// or create a temporary scope for mootools things
(function($) {
$("foo"); // mootools
})(document.id);
best / recent practices in mootools development require plugins and code released to reference document.id or be within such a closure to ensure compatibility. this is actually ok as unlike in jquery where $ aliases the jQuery singleton, in mootools $ is just a selector so its use will be far less spread. Hence typing document.id("selector") is not going to be that much of a drag.

Have you removed all reference to jQuery in your htmls?

MooTools will not override the $ function if it exists already. It checks for nullness of $ before defining it. So I suspect the $ is still lurking somewhere.
if (window.$ == null) Window.implement({
$: function(el, nc){
return document.id(el, nc, this.document);
}
});
After including jQuery and running $.noConflict(); but before including MooTools, can you log the contents of $ and see what is logged?
include jquery
$.noConflict();
console.log($); // should return undefined
include mootools

If you are using both libraries on the same page you must use JQuery's noConflict() function
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
});
// Code that uses other library's $ can follow here.
</script>
If you are still having trouble, try checking through your included JQuery files to ensure that any plugins/code use jQuery('div.canvas') etc instead of $ as $ has been released by the noConflict() function and will not run JQuery code.

Related

type error in jQuery 1.8 and above

This snippet of code;
$(function () {
var tabContainers = $('div.tabs > div');
tabContainers.hide().filter(':first').show();
$('div.tabs ul.tabSelect a').click(function () {
tabContainers.hide();
tabContainers.filter(this.hash).fadeIn('slow');
$('div.tabs ul.tabSelect a').removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(':first').click();
});
Works fine in jQuery 1.5 but throws a "type error $ is not a function" error with jQuery 1.10 (or 1.8)
What am i missing here? It's not the first time it's cropped up with migrating god to later jQuery version.
window.jQuery(function () {
var tabContainers = window.jQuery('div.tabs > div');
tabContainers.hide().filter(':first').show();
window.jQuery('div.tabs ul.tabSelect a').click(function () {
tabContainers.hide();
tabContainers.filter(this.hash).fadeIn('slow');
window.jQuery('div.tabs ul.tabSelect a').removeClass('selected');
window.jQuery(this).addClass('selected');
return false;
}).filter(':first').click();
});
this works just fine. There are no other libraries, just two calls
one
<script type='text/javascript' src='http://localhost:8888/wordpress/wp-includes/js/jquery/jquery.js'></script>
is to 1.10.1 and throws the error
`<script type='text/javascript' src='http://localhost:8888/wordpress/wp-includes/js/jquery/jquery-1.5.1.min.js'></script>`
doesn't.
that's my question. if you don't understand it don't answer it and don't give me minus reputation because you can't be bothered to read my post.
This error does not comes from the upgrade of jQuery, since $ is still available in current versions.
Check you don't forget to include jQuery, that the file path is OK (use your browser's javascript console, accessible by F12 key) and that you didn't include jQuery two times (older and newer versions together).
Another problem could be that there is a jQuery.noConflict(); call somewhere in your code. This function is meant to prevent those conflict between different versions. You can, for example, do $my_jquery = jQuery.noConflict(); and then use it this way $my_jquery('my_selector')...
You've incorrectly loaded the library:
Check if the lib is loaded before the script is called
Check if you've actually loaded jQuery and not a partially downloaded file, bad file name, or other errors on your console exist.
And/or you're loading a second library which replaces the $ global, ie, Prototype. Or, you've re-assigned the $ global.
Or you're just using cut+paste code from a website and you've copied in a similar looking character that is not actually $ and the computer won't treat it as such.
That's the only way you can have this issue.
From the jQuery documentation. A complete and utter solution of the problem.
Aliasing the jQuery Namespace
When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would normally write $. However, the handler passed to the .ready() method can take an argument, which is passed the global jQuery object. This means we can rename the object within the context of our .ready() handler without affecting other code:
jQuery( document ).ready(function( $ ) {
// Code using $ as usual goes here.
});

Writing a custom plugin for Zepto with jQuery fallback

I am looking into Zepto with a jQuery fallback (as seen on Zepto's website) to see if it is viable for an upcoming project.
I was hoping to build a custom plugin using $.extend, but noticed in Zepto's example that they pass in Zepto as the $ variable as does jQuery. Would there be a way to pass in whatever library is loaded?
Zepto plugin:
;(function($){
$.extend($.fn, {
foo: function(){
}
})
})(Zepto)
jQuery plugin:
(function( $ ) {
$.fn.myPlugin = function() {
};
})( jQuery );
Edit: Updated answer.
My original thoughts were to simply pass $ as the parameter, which works, but doesn't offer great portability if the plugin is to be distributed throughout various projects.
Instead you should use the OR operator to detect whether Zepto is loaded, falling back to jQuery if it isn't.
;(function($){
$.extend($.fn, {
foo: function(){
}
})
})(window.Zepto || window.jQuery)
Use jQuery.noConflict() to avoid collision with another library that uses $ alias. That way $ would always be Zepto and you can use any other alias you want for jQuery, or just use the jQuery object itself
You can still use $ in your jQuery code wrapped in document.ready if you pass it as an argument of document.ready;
API Reference: http://api.jquery.com/jQuery.noConflict/

Use of dojo/select on non-AMD code

I have on my site legacy JavaScript that uses Sizzle as selector engine.
I recently added the dojo library (v 1.8) for visualization purposes (charts, etc.). Because dojo includes selectors (dojo/select), I am thinking that Sizzle is now redundant and that I could replace it with dojo/select. Is there a way to make dojo/select work with non-AMD code?
Brandon Boone's answer is very useful so you do not have to rewrite your selector strings. I think what you are asking for is how to export dojo/query into global namespace, i.e. into window object through asynchronous nature of AMD. There are two options:
If you use release version, it has dojo/query already packed in dojo.js, so you do not have to take care of asynchronous execution of module factory function, just export the variable:
<script
src="http://ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js"
data-dojo-config="async:true"
></script>
<script>
// export query module to global namespace (window object)
require(["dojo/query"], function(query) {
window.query = query;
});
</script>
<script>
// query function is now available globally
console.log(query("li"));
</script>
See this example in action at jsFiddle: http://jsfiddle.net/phusick/gvnGu/
If you use baseless dojo, it would be more tricky because you actually have to wait for dojo/query to load:
<script src="dtk-sdk/dojo/dojo.js" data-dojo-config="async:true"></script>
<script>
// wrap your lecacy code into a function so it's not executed immediately
var executeNonAmdCode = function() {
console.log(query("li"));
}
</script>
<script>
require(["dojo/query"], function(query) {
// export query module to global namespace (window object)
window.query = query;
// execute the legacy code
executeNonAmdCode();
});
</script>
According to the docs you can swap out the DOJO selector engine for AMD/Dojo compatible versions of sizzle or slick. So if I were you, I'd keep Sizzle around and change dojo's underlying selector to Sizzle, removing the redundancy without having to touch legacy code.
We can also use other selector engine levels. Both Sizzle and Slick
are excellent selector engines that work with dojo/query. AMD/Dojo
compatible versions (just wrapped with AMD) are available here:
https://github.com/kriszyp/sizzle
https://github.com/kriszyp/slick
Once installed, you can use the selector engine module id as specified
selector engine level. We could set Sizzle as the query engine for our
page:
<script data-dojo-config="selectorEngine: 'sizzle/sizzle'" src="dojo/dojo.js">
</script>
or set Slick as the engine for a particular module:
define(["dojo/query!slick/Source/slick"], function(query){
query(".someClass:custom-pseudo").style("color", "red");
});
To add to #phusick's answer, I just learnt that starting with v1.8 dojo supports this:
<script type="dojo/require">
"myApp.query": "dojo/query"
</script>
The above code will add the query method to the global myApp object (and create myApp if needed).
Source:
http://dojotoolkit.org/reference-guide/1.8/dojo/parser.html#dojo-parser

Jquery No Conflict jquery-1.7.1.min.js

I am not too familiar with jQuery.noConflict. I have tried to implement it a couple times, but I feel I am doing it wrong.
Is there a way to set a noConflict with "jquery-1.7.1.min.js"? Do I put it in the actual file, or in the header of my index or both? I have tried to follow examples but I know I am doing it wrong.
Any guidance or quick examples would help me tremendously!
var foo = $.noconflict();
foo('body').addClass('bar');
You can either assign it a new alias (as shown above) or call $.noConflict and only have jQuery available for use. If you chose an alias though you must use that new alias every time you want to reference jQuery.
Keep in mind though that you can enable noConflict, but still have it available when necessary using an anonymous function:
// disable $ and force use of myJQ
var myJQ = jQuery.noConflict();
(function($){
//
// be able to use $ within this block and have it mean jQuery
//
$('body').addClass('foo');
})(myJQ);
// we're outside the block, now we're back to myJQ
myJQ('body').removeClass('foo');
No conflict mode is easy to use. Include this shortly after loading jQuery and any jQuery-dependent libraries:
var $j = jQuery.noConflict();
Then, instead of using $ for everything, use $j:
var elements = $j('.class-name');
Have you tried the following examples:
http://api.jquery.com/jQuery.noConflict/
I think it says all about it. Check your browser console to see any errors.
I'd recommend using a noConflict call, as well as wrapping your jQuery code with an anonymous function, so you can call jQuery by $:
jQuery.noConflict();
(function ($) {
// Now you can use $ to call jQuery
}(jQuery));

Declare Code as JQuery

I have mootools and jquery installed (Joomla) for various reasons, and I think my jquery code is trying to run through mootools because I keep getting a bunch of garbage errors with $.getJSON. Is there something I can wrap around my Jquery code that I don't have this kind of cross-framework conflict?
You need to use noConflict():
http://api.jquery.com/jQuery.noConflict/
You can replace $ with any variable you want:
var $jq = jQuery.noConflict();
If you read the doc, you can use noConflict and still use $ within a $.ready() call, and use $ for mootools outside of the ready call.
var jQuery = jQuery.noConflict();
then try replace the $ with jQuery
or alternatively just jQuery.noConflict(); and keep using $
Check this

Categories

Resources