jQuery and other JavaScript frameworks
jQuery uses the $ sign as a shortcut for jQuery, there could be conflict if other JavaScript frameworks also use the $ sign as a shortcut.
Other popular JavaScript frameworks are :
- MooTools
- Backbone
- Sammy
- Cappuccino
- Knockout
- JavaScript MVC
- GWT (Google Web Toolkit)
- Google Closure
- Ember
- Batman
- Ext JS
Some of the frameworks also use the $ character as a shortcut (just like jQuery), using them togather might result in that your scripts stop working.
noConflict()
noConflict() method in jQuery releases the hold on the $ shortcut identifier, so that other scripts can use it.
You can of course still use jQuery, simply by writing the full name instead of the shortcut.
Example
$.noConflict();
jQuery(document).ready(function() {
jQuery("button").click(function() {
jQuery("p").text("jQuery is still works");
});
});
You can also create your own shortcut very easily.
The noConflict() method returns a reference to jQuery, that you can save in a variable, for later use.
Example
var jq = $.noConflict();
jq(document).ready(function() {
jq("button").click(function() {
jq("p").text("jQuery still works");
});
});
If you have a block of jQuery code which uses the $ shortcut and you do not want to change it all, you can pass the $ sign in as a parameter to the ready method.
This allows you to access jQuery using $, inside this function - outside of it, you will have to use "jQuery".
Example
$.noConflict();
jQuery(document).ready(function($) {
$("button").click(function() {
$("p").text("jQuery still works");
});
});
No comments:
Post a Comment
Note: only a member of this blog may post a comment.