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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:36:12+00:00 2026-05-18T21:36:12+00:00

I often need to load other javascript files via ajax, so at the beginning

  • 0

I often need to load other javascript files via ajax, so at the beginning I used the standard function jQuery provides for script loading:

$.getScript('script_name.js',callback_function());

But this didn’t work out, since $.getScript is asynchronous (the jQuery API for $.ajax says ‘async’ is set to true by default; topic is discussed in the comments of the API for $.getScript: http://api.jquery.com/jQuery.getScript/). So I wrote this function, as provided by someone in the comments of the API page linked above:

load:function(script,callback){

    jQuery.ajax({

        async:false,

        type:'GET',

        url:script,

        data:null,

        success:callback,

        dataType:'script'

    });

},

This seemed to work well, so I went on, but I recently noticed, that this only works for scripts in the same directory, eg. calling myObj.load(‘test.js’) works well, but calling myObj.load(‘test/test.js’) doesn’t work at all.

It feels like I’m missing something obvious, but I didn’t manage to find the problem. Any idea?

  • 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-18T21:36:13+00:00Added an answer on May 18, 2026 at 9:36 pm

    Update: See the comment stream below, it’s nothing to do with jQuery, it’s a file permissions problem on the server.


    Original answer:

    Do you get any errors from the browser? For instance, in Chrome or Safari, if you open the Dev Tools and look at the Console tab, does it show errors? Or in Firefox, install Firebug and check in Firebug’s console. Or in IE, use the free version of VS.Net… Something should be complaining to you about something.

    You can also get more information from your code itself by supplying an error function rather than assuming success:

    jQuery.ajax({
        async:false,
        type:'GET',
        url:script,
        data:null,
        success:callback,
        dataType:'script',
        error: function(xhr, textStatus, errorThrown) {
            // Look at the `textStatus` and/or `errorThrown` properties.
        }
    });
    

    Update: You’ve said you see textStatus = ‘error’ and errorThrown = undefined. Very strange. Does the same script work if you move it so it’s not on a subpath? I wonder if the subpath is a red herring, and the real problem is a syntax error in the script.


    Off-topic: Does it really have to be synchronous? You can’t just poll for a symbol to appear? It’s just that synchronous ajax requests really trash the user experience. In many browsers, not just your own page but all pages lock up during the request.

    Here’s what I mean by polling: Suppose I wanted to load jQuery asynchronously from JavaScript:

    function loadScript(url, symbol, callback) {
        var script, expire;
    
        // Already there?
        if (window[symbol]) {
            setTimeout(function() {
                callback('already loaded');
            }, 0);
        }
    
        // Determine when to give up
        expire = new Date().getTime() + 20000; // 20 seconds
    
        // Load the script
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;
        document.body.appendChild(script);
    
        // Start looking for the symbol to appear, yielding as
        // briefly as the browser will let us.
        setTimeout(lookForSymbol, 0);
    
        // Our symbol-checking function
        function lookForSymbol() {
            if (window[symbol]) {
                // There's the symbol, we're done
                callback('success');
            }
            else if (new Date().getTime() > expire) {
                // Timed out, tell the callback
                callback('timeout');
            }
            else {
                // Schedule the next check
                setTimeout(lookForSymbol, 100);
            }
        }
    }
    

    Usage:

    // Load jQuery:
    loadScript("path/to/jquery.min.js", "jQuery", function(result) {
        // Look at 'result'
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While debugging jQuery apps that use AJAX, I often have the need to see
I get .dmp files every so often that I need to load in a
I need to load a web page, execute its JavaScript (and all js files
I often need to use a function which performs and action X is condition
I often need to fetch tgz files, decompress them, and then delete the tgz.
Brief summary: I need a script/plugin for Firefox that selects the load next 25
In vim, when I have a buffer open, I often need to load another
I need to call a function after page load, but only once, page elements
In my update pages I have often the need to call the same jQuery
I often need relatively small (<10000 entries <1kb) caches for speeding up calculations. My

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.