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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
I'm trying to implement the GetHttpContext function from HtmlHelperTest.cs in VB.NET using Rhino Mocks,
I have difficulty in using the vb code dom. Basically, I want to compile
I am using JQuery & JSON (POST) to call webmethod. However I can call
Using the below XML, I need to figure out which person worked more hours
I can't figure this out as I go through demos that seem to work.
I have written a simple log4net wrapper. I was wondering whether this wrapper code
A few weeks ago, I was able to build Apps using py2app. I just
I'm writing some unittests for code written by someone else here at the office.
Using SVN you can right click a folder TEST and add it to the

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.