How to use jQuery in Firefox Extension

I want to use jQuery inside a firefox extension, I imported the library in the xul file like this:

<script type="application/x-javascript" src="chrome://myExtension/content/jquery.js"> </script>

but the $() function is not recognized in the xul file neither do the jQuery().

I googled about the problem and found some solutions but no one did work with me:

I've also tried to pass the 'content.document' object(which refrences the 'document' object) as the context parameter to the jQuery function like this:


but still not working, does any one came across this problem before?



Turns out the current top-answer by @sunsean does not work as expected when it comes to handling multiple loads. The function should properly close over the document and avoid global state.

Also, you have to call jQuery.noConflict(true) to really avoid conflicts with other add-ons!

This is who I would write it (then again, I would avoid jquery (in add-ons) like the plague...). First the overlay XUL

<?xml version="1.0"?>
<overlay id="test-addon-overlay" xmlns="">
  <script type="text/javascript" src="jquery.js"/>
  <script type="text/javascript" src="overlay.js"/>

And then the overlay script:

// Use strict mode in particular to avoid implicitly var declarations
(function() {
  "use strict";

  // Main runner function for each content window.
  // Similar to SDK page-mod, but without the security boundaries.
  function run(window, document) {
    // jquery setup. per
    $ = function(selector,context) {
      return new jq.fn.init(selector,context || document); 
    $.fn = $.prototype = jq.fn;

    if (document.getElementById("my-example-addon-container"))  {
    let main = $('<div id="my-example-addon-container">');
    main.appendTo(document.body).text('Example Loaded!'); { //<--- added this function

  const log = Components.utils.reportError.bind(Components.utils);

  // Do not conflict with other add-ons using jquery.
  const jq = jQuery.noConflict(true);

  gBrowser.addEventListener("DOMContentLoaded", function load(evt) {
    try {
      // Call run with this == window ;)
      let doc = ||;
      if (!doc.location.href.startsWith("http")) {
        // Do not even attempt to interact with non-http(s)? sites.
      }, doc.defaultView, doc);
    catch (ex) {
  }, true);

Here is a complete add-on as a gist. Just drop in a copy of jquery and it should be good to go.


It may be bad practice, but have you considered including it inline?


Instead of


you can try


In my case it works.


I use the following example.xul:

<?xml version="1.0"?>
<overlay id="example" xmlns="">
<script type="application/x-javascript" src="jquery.js"></script>
<script type="application/x-javascript" src="example.js"></script>

And here is an example.js

(function() {
    $ = function(selector,context) { 
        return new jQuery.fn.init(selector,context||example.doc); 
    $.fn = $.prototype = jQuery.fn;

    example = new function(){};
    example.log = function() { 
    }; = function(doc,aEvent) {
        // Check for website
        if (!doc.location.href.match(/^http:\/\/(.*\.)?stackoverflow\.com(\/.*)?$/i))  

        // Check if already loaded
        if (doc.getElementById("plugin-example")) return;

        // Setup =;
        this.doc = doc;

        // Hello World
        this.main = main = $('<div id="plugin-example">').appendTo(doc.body).html('Example Loaded!');
        main.html(main.html() + ' - jQuery <b>' + $.fn.jquery + '</b>');

    // Bind Plugin
    var delay = function(aEvent) { 
        var doc = aEvent.originalTarget; setTimeout(function() { 
        }, 1); 
    var load = function() { 
        gBrowser.addEventListener("DOMContentLoaded", delay, true); 
    window.addEventListener("pageshow", load, false);


The following solution makes it possibile to use jQuery in contentScriptFile (Targetting 1.5 Addon-sdk)

In your main.js:

exports.main = function() {
    var pageMod = require("page-mod");

          include: "*",
          contentScriptWhen: 'end',
          contentScriptFile: [data.url("jquery-1.7.1-min.js") , data.url("notifier.js") ,   data.url("message.js")],
          onAttach: function onAttach(worker) {
             //show the message
             worker.postMessage("Hello World");


In your message.js :

self.on("message", function(message){
    if(message !== "undefined"){; 

Some pitfalls you need to watchs out for:

  • The order of the contentScriptFile array. if message.js would be placed first: jQuery won't be reconized.
  • Do not place a http:// url in the data.url (this does not work)!
  • All your javascript files should be in the data folder. (only main.js should be in lib folder)

There is an excellent article in the mozillaZine forums that describes this step-by-step:

I haven't tried it yet, though so I hesitate to duplicate the info here.


I think this is what Eric was saying, but you can load Javascript from the URL directly.


Im assuming you want your extension to load JQuery so you can manipulate the page elements easily? My company's labs has something that does this using Javascript directly here:


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.