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 8068305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:37:48+00:00 2026-06-05T12:37:48+00:00

Does anybody know a way to detect if the jQuery library was loaded and

  • 0

Does anybody know a way to detect if the jQuery library was loaded and if not append it and start a script as a fallback solution after it’s loaded to the DOM?

Here’s my script:

if (typeof jQuery == 'undefined') {
    // if jQuery Library is not loaded
    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
    document.body.appendChild(script);
    startScript();
} else {
    // cool, jQuery Library is loaded
    startScript();
}

function startScript() {
    $.noConflict();
    jQuery(document).ready(function($){
    .... // my jquery code
    });
}

Of course, above appends jQuery correctly but doesn’t do anything because the script starts immidiately after appending. Any hint on this?

I appreciate any answer. Thanks!

  • 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-05T12:37:54+00:00Added an answer on June 5, 2026 at 12:37 pm

    You’re fine except, as you said, that you can’t call startScript immediately after appending the new script element. Instead, do a setTimeout loop:

    // ...
    document.body.appendChild(script);
    maybeStart();
    

    Where maybeStart looks like this (not within the body of the if, function declarations aren’t valid there):

    function maybeStart() {
        if (typeof jQuery === "undefined") {
            setTimeout(maybeStart, 100);
        }
        else {
            startScript();
        }
    }
    

    That checks if jQuery is loaded and, if not, waits a 10th of a second and checks again. When jQuery is found, it calls your startScript.

    You might want to put a time limit in there (in case jQuery never loads, so you don’t loop forever).

    Alternately, you can hook a “load”-style event on the script element, but the mechanics of it vary from browser to browser. On some browsers it’s just load as with, say, an img element; on IE (at least older versions), you have to use readystatechange, and on some versions of IE both work. So you have to keep a flag so you know whether you’ve fired off your script, etc., and…well, the above is less complicated in the case where you know the script creates a well-known global symbol (as is the case with jQuery).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know of an automated way of telling whether a PHP script is
Does anybody know a way of doing this (at runtime)? I'm not sure I
Does anybody know if there is a simple way to detect character set encoding
Does anybody know a way to embed an icon in a Python script such
Does anybody know a way to make the axis fixed on a CPTXYGraph when
Does anybody know offhand the way to tell whether I'm being called statically (Classname::function)
Does anybody know of a way of generating a boxplot in R with a
Does anybody know of a way to use Guice to inject dependencies into the
Does anybody know of a way to create a DataSet such that when I
Using .NET's Office interop libraries, does anybody know the best way to convert back

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.