{"id":843,"date":"2013-02-03T01:17:07","date_gmt":"2013-02-02T23:17:07","guid":{"rendered":"https:\/\/blog.zitnik.si\/?p=843"},"modified":"2013-02-03T01:17:07","modified_gmt":"2013-02-02T23:17:07","slug":"how-to-integrate-two-independent-jquery-libraries-within-a-single-page","status":"publish","type":"post","link":"https:\/\/blog.zitnik.si\/?p=843","title":{"rendered":"How to integrate two independent jQuery libraries within a single page?"},"content":{"rendered":"<p>It has been long since my last post. That is not because I have nothing useful to write, but more likely that I am lazy to document interesting stuff I do&#8230;. Anyway, let&#8217;s go to the point!<\/p>\n<p>Recently I had to integrate <a href=\"http:\/\/lokeshdhakar.com\/projects\/lightbox2\/\" target=\"_blank\">Lightbox 2<\/a> into a website that is run by a CMS. There was no possibility of FTP access, PHP source code access, &#8230; The only thing I could use was CMS&#8217;s static content form.<\/p>\n<p>Surprisingly I could add the following code as a HTML form content:<\/p>\n<p><code><br \/>\n&lt;script type=\"text\/javascript\" src=\"http:\/\/zitnik.si\/temp\/lightbox\/js\/jquery-1.7.2.min.js\"&gt;&lt;\/script&gt;<br \/>\n&lt;script type=\"text\/javascript\"&gt;<br \/>\n\/\/ &lt;![CDATA[<br \/>\ndocument.write(\"&lt;link href='http:\/\/zitnik.si\/temp\/lightbox\/css\/lightbox.css' rel='stylesheet' \/&gt;\");<br \/>\n\/\/ ]]&gt;<br \/>\n&lt;\/script&gt;<br \/>\n&lt;script type=\"text\/javascript\" src=\"http:\/\/zitnik.si\/temp\/lightbox\/js\/lightbox.js\"&gt;&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>These lines just load appropriate <a href=\"http:\/\/jquery.com\/\" target=\"_blank\">jQuery<\/a> library (currently 1.7.2, used by Lightbox 2), CSS styles and finally Lightbox&#8217;s JavaScript code. Due to the fact I could not insert link tags directly into the content, I accomplished this by printing the code using JavaScript. The latter does not influence on having multiple jQuery libraries on a page, but it needed to be done in my case and seems a nice workaround :).<\/p>\n<p>The problem was that CMS is using jQuery of version 1.5.2, but Lightbox needed 1.7.2. Because I could not upgrade 1.5.2 version to the latest, I had to separate these two libraries. I also could not just simply override the old one because other parts of CMS generated page stopped working. This can be achieved by loading the second library into a variable. Into the upper javascript I added the following:<br \/>\n<code><br \/>\nvar $jq172 = jQuery.noConflict(true);<br \/>\n<\/code><br \/>\nAs you maybe know, the jQuery functions can be called by <em>$()<\/em>. After this command, the formerly loaded jQuery can be accessed through <em>$()<\/em> and last added library as <em>$jq172()<\/em>. If the parameter to the noConflict function is true, the previous library is intact, otherwise it is overwritten.<\/p>\n<p>Lastly I needed to apply minor change to lightbox.js to instruct the script to use 1.7.2 library. Due to the nice coding style I just needed to change the line 46 into<br \/>\n<code><br \/>\n$ = $jq172;<br \/>\n<\/code><br \/>\nBy applying all these I was able to have working Lightbox 2, working previous CMS scripts and having done everything without ant CMS code change. The Lightbox library can be then used completely normally.<\/p>\n<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div>","protected":false},"excerpt":{"rendered":"<p>It has been long since my last post. That is not because I have nothing useful to write, but more likely that I am lazy to document interesting stuff I do&#8230;. Anyway, let&#8217;s go to the point! Recently I had to integrate Lightbox 2 into a website that is run&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/blog.zitnik.si\/?p=843\">Continue reading<span class=\"screen-reader-text\">How to integrate two independent jQuery libraries within a single page?<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-843","post","type-post","status-publish","format-standard","hentry","category-computer-engineering","entry"],"_links":{"self":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=843"}],"version-history":[{"count":7,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/843\/revisions"}],"predecessor-version":[{"id":850,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/843\/revisions\/850"}],"wp:attachment":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}