Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:07:53+00:00 2026-06-02T05:07:53+00:00

In my userscripts, I use this code to get jQuery and rest of my

  • 0

In my userscripts, I use this code to get jQuery and rest of my code injected and executed:

function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}
function readyJQuery() {
  jQuery.noConflict();
  jQuery(document).ready(function() {
    //my rest code goes here
  })
}
addJQuery(readyJQuery)

This works perfectly. But when I have two scripts they both inject the jQuery part. The scripts need to be separate, because I don’t know who wants to use which script or both scripts.

Can I prevent the second script from injecting jQuery and use the one from the first script?

When the second script directly starts with:

function addJQuery(callback) {
  var script = document.createElement("script");
  script.textContent = "(" + callback.toString() + ")();";
  document.body.appendChild(script);
}
function readyJQuery() {
  jQuery.noConflict();
  jQuery(document).ready(function() {
    //my rest code goes here
  })
}
addJQuery(readyJQuery)

I get the error Message in the console: jQuery is undefined.
How can I test if a different script already injected jQuery and then wait until it’s ready?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:07:54+00:00Added an answer on June 2, 2026 at 5:07 am

    Give your jQuery a unique identifier, check for that identifier, and only add jQuery if it is not found. You’ll also need to allow enough to time for the first jQuery to initialize.

    Changing addJQuery() to the following, in both scripts, works in my tests:

    function addJQuery (callback) {
        //--- Has jQuery already been added?
        var jqNode  = document.querySelector ("#myAddedJQ");
        if (jqNode) {
            FireCallback (callback);
        }
        else {
            //--- Wait a bit to be sure.
            setTimeout ( function() {
                    var jqNode  = document.querySelector ("#myAddedJQ");
                    if (jqNode) {
                        FireCallback (callback);
                    }
                    else {
                        var script  = document.createElement ("script");
                        script.id   = "myAddedJQ";
                        script.setAttribute (
                            "src",
                            "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
                        );
                        script.addEventListener ('load', function () {
                            FireCallback (callback);
                        }, false);
                        document.body.appendChild (script);
                    }
                }
                , 444
            );
        }
    
        function FireCallback (callback) {
            var script          = document.createElement ("script");
            script.textContent  = "                                         \
                if (typeof waitForJqDefined == 'undefined') {               \
                    var waitForJqDefined    = setInterval ( function () {   \
                            if (typeof jQuery != 'undefined') {             \
                                clearInterval (waitForJqDefined);           \
            ";
            script.textContent += "\n(" + callback.toString() + ")();\n";
            script.textContent += "                                         \
                            }                                               \
                        },                                                  \
                        50                                                  \
                    );                                                      \
                }                                                           \
                else {                                                      \
            ";
            script.textContent += "\n(" + callback.toString() + ")();\n";
            script.textContent += "                                         \
                }                                                           \
            ";
            document.body.appendChild (script);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add jquery-ui to a Greasemonkey script. my full code: test.user.js :
I am attempting to use Greasemonkey and jQuery 1.4.2. When I install the Greasemonkey
I use a site that looks like this: ... <body> <iframe src=blabla/blabla...></iframe> </body> ...
I have a private script that I'm trying to get to run as a
I'm rewriting an userscript and replacing the use of dojo with jQuery, but then
I'm a developer of http://userscripts.org/scripts/review/86621 , a simple script, which allows to monitor your
I tried to figure this out for some days now, I tried to use
Here is the Greasemonkey script I'm working on (source below): http://userscripts.org/scripts/show/69722 You can test
When I use a setTimeout() in a for() loop in a greasemonkey script, it
For this userscript I'm writing, I need to use a third-party JavaScript library which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.