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开发者_Python百科/scripts/jquery/js/jquery-1.4.4.min.js"/>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function () {
alert("hello");
});
</script>
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
Does this alert hello
?
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function () {
alert("hello");
});
</script>
If it does, then jQuery is working. This seems like really trivial answer, sorry.
精彩评论