Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use jQuery in firefox addon?

I want to use jQuery in the sidebar of my firefox extension. This is how I include jQuery in the sidebar.xul

  <script type="application/x-javascript" src="chrome://myaddon/content/
  scripts/jquery/js/jquery-1.4.4.min.js"/>

  <script type="text/javascript">jQuery.noConflict();</script>

First question, why do we use the jQuery.noConflict() function?

I tried the solutions of some other questions but it did not work for me.

Still this does not work for me on FF 3.6.13:

<script type="application/x-javascript" 
       src="chrome://myextension/content/scripts/jquery/js/jquery-1.4.4.min.js"/>
  <script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function () {
        alert("hello");
    });
 </script>
like image 783
UpCat Avatar asked Oct 25 '22 19:10

UpCat


1 Answers

When you include other libraries like prototype, mootools, YUI etc, The problem comes when one or more of those libraries are used with jQuery as they also use $() as their global function and to define variables. This situation creates conflict as $() is used by jQuery and other library as their global function. To overcome from such situations, jQuery has introduced jQuery.noConflict().

So you can basically do

 var $j = jQuery.noConflict();

Now you can write jQuery command using $j() instead of $()

As for the firefox plugin, I'm assuming you are referring to firebug. The area where you type in your jquery is called console. You can read more at http://getfirebug.com/commandline

enter image description here

like image 151
Hussein Avatar answered Oct 27 '22 09:10

Hussein