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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:35:40+00:00 2026-05-15T17:35:40+00:00

When using an importjs() type of function (see below for an example), jQuery doesn’t

  • 0

When using an importjs() type of function (see below for an example), jQuery doesn’t seem to be loading before the code following it.

Here’s a sample html file:

<html>
  <head></head>
  <body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      function importjs(jsFile) {
        var body = document.getElementsByTagName('head').item(0);
        var scpt = document.createElement('script');
        scpt.src = jsFile;
        scpt.type = 'text/javascript';
        body.appendChild(scpt);
      }
      var f1="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js";
      //importjs(f1);
      var $j=jQuery;
      alert("hello stackoverflow!");
    </script>
  </body>
</html>

With the above code, the alert should successfully fire.

Next, comment out the first script block, i.e. the one explicitly loading jQuery, and uncomment the importjs(f1) line in the second script block. This time, the alert does not fire, at least in firefox and safari.

Now, put in an extra alert before the line “var $j=jQuery”. For me, it works in both browsers, regardless of how long or short I wait. A setTimeout would probably also do the trick, but it’s also not an ideal way to program something like this.

If javascript is single-threaded, why does the importjs fail? Is it because the new element created by importjs doesn’t get ‘executed’ until the first block finishes, or should the new element be executed as soon as it is created?

  • 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-15T17:35:41+00:00Added an answer on May 15, 2026 at 5:35 pm

    There are several problems here:

    • you have jQuery duplicated, one in the html, one in the js
    • dynamically added javascript won’t be available immediately
      if you load scripts this way the dependant code should be in a callback function

      function importjs(jsFile, callback) {
          var head = document.getElementsByTagName('head')[0];
          var script = document.createElement('script');
          script.src = jsFile;
          script.type = 'text/javascript';
          script.onload = script.onreadystatechange = function() {
            // execute callback
            if (callback) callback();
            // prevent memory leak in IE
            script.onload = null;
            head.removeChild(script);
          };
          head.appendChild(script);
      }
      

      then you should use it as:

      importjs("jquery.js", function(){
          // all jQuery dependant code
          // goes here...
      });​
      

    UPDATE

    There is a more robust solution for including javascript files which allows you to:

    • include multiple files that are related
    • ensure they are executed in order
    • load them in a non-blocking way (parallel with other resources)

    I’m still working on this script, but pretty much works right now. Be sure to check it out.

    It combines the advantages of different techniques to give a huge benefit on page load time. Here is a related article: Loading Scripts Without Blocking

    The syntax is:

    include(['jquery.js','jquery-ui.js'], myjQueryCode); // executed in order
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code sample that uses MEF to create an object of type
The code pasted below does the following: creates an import hook creates a context
I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
I am using JQuery & JSON (POST) to call webmethod. However I can call
Possible Duplicate: SendInput and 64bits I'm using SendInput from .NET code (PInvoke). code used
I have difficulty in using the vb code dom. Basically, I want to compile
I can't get my application to work using MVC architecture. Here is the code:

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.