<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>marcuscavalcanti.net &#187; jQuery</title>
	<atom:link href="http://www.marcuscavalcanti.net/blog/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marcuscavalcanti.net/blog</link>
	<description>Software, tecnologia e etc.</description>
	<lastBuildDate>Wed, 21 Jul 2010 03:52:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Como usar jQuery sem conflito com outras bibliotecas JavaScript</title>
		<link>http://www.marcuscavalcanti.net/blog/2009/02/17/como-usar-jquery-sem-conflito-com-outras-bibliotecas-javascript/</link>
		<comments>http://www.marcuscavalcanti.net/blog/2009/02/17/como-usar-jquery-sem-conflito-com-outras-bibliotecas-javascript/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 04:47:30 +0000</pubDate>
		<dc:creator>Marcus Cavalcanti</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://www.marcuscavalcanti.net/blog/?p=324</guid>
		<description><![CDATA[Estava trabalhando em uma página que já fazia o uso da biblioteca jQuery para apresentação de alguns componentes, contudo surgiu a necessidade de usar ajax nessa mesma página. Apesar da jQuery possuir um bom suporte para ajax, eu gosto muito também da prototype, pois além de ter mais prática (com ajax), também já tenho algumas [...]]]></description>
			<content:encoded><![CDATA[<p>Estava trabalhando em uma página que já fazia o uso da biblioteca <a href="http://www.jquery.com" target="_blank" style="text-decoration: underline">jQuery</a> para apresentação de alguns componentes, contudo surgiu a necessidade de usar ajax nessa mesma página. Apesar da jQuery possuir um <a href="http://docs.jquery.com/Ajax" target="_blank" style="text-decoration: underline">bom suporte para ajax</a>, eu gosto muito também da <a href="http://www.prototypejs.org/" target="_blank" style="text-decoration: underline">prototype</a>, pois além de ter mais prática (com ajax), também já tenho algumas customizações prontas.</p>
<p>Após incluir a prototype e testar a página, percebi que um erro de JavaScript estava ocorrendo, no começo achei que a causa do erro era em razão de uma função &#8211; usando a prototype &#8211; que eu tinha acabado de fazer, mas depois usando o bem dito <a href="http://getfirebug.com/" target="_blank" style="text-decoration: underline">firebug</a> e fazendo alguns <em>debugs</em>, percebi que o erro era mesmo em um conflito entre a jQuery e a prototype.</p>
<p>O problema ocorre, pois as duas bibliotecas fazem o uso da função &#8220;$()&#8221; para acessar as suas funções, o que acaba gerando o conflito.</p>
<p>A biblioteca jQuery fornece algumas maneiras de resolver isso, mas todas giram em torno da função <a href="http://docs.jquery.com/Core/jQuery.noConflict" target="_blank" style="text-decoration: underline">jQuery.noConflict()</a>. A solução que oferece o maior custo x benefício na minha opinião, é a que eu não preciso alterar os meus métodos já existentes, ou seja, não precisaria ter que fazer isso:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Javascript"><div class="devcodeoverflow"><ol><li></li><li><span style="color: #003366; font-weight: bold;">var</span> $j <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>$j<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>No caso acima, se eu já tivesse N funções usando jQuery, eu precisaria fazer uma <a href="http://pt.wikipedia.org/wiki/Refatora%C3%A7%C3%A3o" target="_blank" style="text-decoration: underline">refatoração</a> do meu código para mudar o método &#8220;chamador&#8221; das minhas funções de $() para $j(), isso geraria algum tempo perdido com muitas mudanças e muitos testes. A solução que encontrei pede apenas para que as funções fiquem dentro da <em>document ready area </em> da jQuery, então ficaria mais ou menos assim:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Javascript"><div class="devcodeoverflow"><ol><li><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;prototype.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></li><li>&nbsp;&nbsp; <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></li><li>&nbsp;&nbsp; <span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp; jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #006600; font-style: italic;">// Put all your code in your document ready area</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp; jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #006600; font-style: italic;">// Do jQuery stuff using $</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #006600; font-style: italic;">// Use Prototype with $(...), etc.</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'someid'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></li><li> <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Da maneira acima eu não precisaria mudar nenhuma função já existente, bastaria apenas incluí-las dentro do bloco indicado.</p>
<p>Segundo a <a href="http://docs.jquery.com/Using_jQuery_with_Other_Libraries" target="_blank" style="text-decoration: underline">documentação</a>, essa seria a solução mais adequada na maioria dos casos.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marcuscavalcanti.net/blog/2009/02/17/como-usar-jquery-sem-conflito-com-outras-bibliotecas-javascript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
