{"id":430,"date":"2009-07-07T10:40:18","date_gmt":"2009-07-07T15:40:18","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=430"},"modified":"2009-07-07T10:40:18","modified_gmt":"2009-07-07T15:40:18","slug":"runing-jquery-with-other-frameworks-via-noconflict","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/runing-jquery-with-other-frameworks-via-noconflict\/","title":{"rendered":"Running jQuery with Other Frameworks Via noConflict"},"content":{"rendered":"<p><a href=\"http:\/\/www.flickr.com\/photos\/romainguy\/230416692\/\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2009\/07\/chess_romainguy.jpg\" alt=\"Photo &quot;Chess&quot; by Romainguy. Used under a Creative Commons license.\" title=\"Photo &quot;Chess&quot; by Romainguy. Used under a Creative Commons license.\" width=\"579\" height=\"250\" class=\"size-full wp-image-432\" \/><\/a><\/p>\n<p>While <a href=\"http:\/\/jquery.com\/\">jQuery<\/a> is certainly a popular JavaScript framework, it&#8217;s by no means the only game in town. Other frameworks such as <a href=\"http:\/\/www.prototypejs.org\/\">Prototype<\/a>, <a href=\"http:\/\/mootools.net\/\">MooTools<\/a>, <a href=\"http:\/\/www.dojotoolkit.org\/\">Dojo<\/a> and many others all have their own strengths, weaknesses, and devoted groupies.<\/p>\n<p>Generally speaking, these frameworks all play well together &mdash; you can mix and match framework functionality to your heart&#8217;s content, as long as you don&#8217;t mind the additional overhead of loading several libraries simultaneously. So you have a calendar widget in jQuery that you love, but you&#8217;re already using Prototype to animate your navigation bar? Don&#8217;t be shy&#8230; use both!<\/p>\n<p>Of course, every once in a while you can run in to problems when combining JS frameworks &mdash; particularly (in my experience) when combining jQuery and Prototype.  Luckily, jQuery was kind enough to provide us with a workaround.<!--more--><\/p>\n<h3>The Problem: Sharing Syntax<\/h3>\n<p>The most common compatibility problem stems from both jQuery and Prototype using the same shortcut syntax: namely, the <em>$().doSomething<\/em> syntax. Here&#8217;s a sample line of code in jQuery:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$('#myelement').addClass('active');<\/pre>\n<p>And the same functionality in Prototype:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$('myelement').addClassName('active');<\/pre>\n<p>Note the basic similarity? Both frameworks claim the dollar sign notation for themselves, which can wreak havoc on snippets of code dropped willy-nilly into a website. If your jQuery code is grabbed up by Prototype, things will stop working fast. And similarly, if your Prototype code is snagged by jQuery, not even the awesome power of jQuery will be enough to overcome the code confusion.<\/p>\n<h3>The Solution: noConflict Mode<\/h3>\n<p>But not to worry! jQuery has provided us with a workaround called &#8220;noConflict mode.&#8221;<\/p>\n<p>By default, there are two equally correct ways to call a jQuery function &mdash; the dollar sign notation, and &#8220;jQuery&#8221; notation:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$('#myelement').show();\njQuery('#myelement').show();<\/pre>\n<p>Both of the lines above do exactly the same thing. However, most people use and prefer the dollar sign notation. Why? Probably because it&#8217;s shorter, and if web developers didn&#8217;t care about brevity in their code, they probably wouldn&#8217;t have used a framework in the first place.<\/p>\n<p>Of course, just using the longer jQuery notation isn&#8217;t enough. If jQuery has already claimed the dollar sign for itself, any Prototype functionality relying on that notation will still be grabbed by jQuery. <\/p>\n<p>This is where the noConflict function comes in handy. Simply run the following line after both Prototype and jQuery have been loaded:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">jQuery.noConflict();<\/pre>\n<p>This will cause jQuery to give up the dollar-sign notation, allowing the other library to take it over. And you can still use your jQuery snippet, provided you change all instances of $() to jQuery().<\/p>\n<h3>Keeping it Short<\/h3>\n<p>The noConflict mode does have one other bit of functionality that I&#8217;ve found useful in some of my projects: you can select a different variable to use instead of the standard &#8220;jQuery&#8221;. The usage looks like this:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">var $j = jQuery.noConflict();<\/pre>\n<p>Now in addition to using the default jQuery() notation, I can also use the shorter $j() notation. This allows me to avoid running into problems with other frameworks, while still enjoying almost the same conciseness in my code.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>While there&#8217;s nothing to stop you from using multiple frameworks simultaneously, compatability problems can pop up &mdash; particularly between jQuery and Prototype. Luckily, there&#8217;s a simple solution called noConflict mode. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/runing-jquery-with-other-frameworks-via-noconflict\/\" title=\"Click to read 'Running jQuery with Other Frameworks Via noConflict'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168,170,180,184],"tags":[248,55,291,293,305,308,309,329],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/430"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}