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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:34:38+00:00 2026-05-29T15:34:38+00:00

I saw similar question here in SO, where someone wants to do the same

  • 0

I saw similar question here in SO, where someone wants to do the same thing. The different case is that I have this lightweight script that listens for event bubbling to the top of the document root that I intentionally want to put on the top of the page and I dont want to wrap it inside $(document).ready(). It looks something like this:

<html>
<head>
    <script>
        document.documentElement.onclick = function (e) {
        //$.ajax call using jQuery. Possible that jQuery is not loaded when page is not completely loaded
        }
    </script>
</head>

And then near the bottom of the page I include jQuery like this:

<body>
    <script src="./jQuery.js"></script>
</body>
</html>

As you can see on the <head> when I’m making an ajax call to somewhere using $.ajax, it’s possible that jQuery is not yet loaded, so that it may throw $ is undefined error when someone clicks on something on DOM, and jQuery is not loaded yet. My workaround is probably to use settimeout hoping that jquery will be loaded sometime later and the code is working like this:

if (typeof $ === 'undefined'){
  setTimeout(doAJAXJquery, 50);
}else{
  $.ajax(//blablabla) //jQuery loaded and ready to use!!
}

But it’s a really dirty hack. How am I supposed to know when jQuery is finished loading? that 50ms i put there is only an arbitrary value i made up myself. Of course it is still not gonna work if jquery hasn;t been loaded yet and it already passes 50ms.

Is there any better workaround so when jQuery is undefined, it is gonna retry sometime later again, or put a function that executes ajax inside some callback that gets executed when jQuery is ready? 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-05-29T15:34:39+00:00Added an answer on May 29, 2026 at 3:34 pm

    what about

    <head>
    <script type="text/javascript">
    var init = function(){
        document.documentElement.onclick = function (e) {
        //$.ajax call using jQuery. Possible that jQuery is not loaded when page is not completely loaded
        }
    }
    </script>
    </head>
    <body>
    <script src="./jQuery.js"></script>
    <script type="text/javascript">
    init();
    </script>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this similar question here but can't figure out how to use Contains
I saw that some others faced this similar problem. I have read and checked
I saw a question that is similar to my one but not the same,
I saw another similar question answered here - Velocity editor plugin for Eclipse? .
I'm trying to solve interviewstreet's median challenge. I saw a similar question posted here:
I saw a similar question being posted here, yet it did not help me
I saw a similar question asked and answered for ASP.net here How do I
I have a question involving the Replace Method. I saw a question similar to
I saw the other question here with the similar error yet their fixes did
I saw this answer to a similar question , but it always scares me

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.