0

I am using this answer, the ever popular SO post to dynamically load a javascript file:

$.getScript("scripts/datagrid.js", function(){

    alert("Script loaded but not necessarily executed.");                      

});

But the it never loads. JQuery is referenced in the underlying HTML file in the header before the js file that is calling this script loading function. Furthermore, I don't call this script loader until after the document is loaded:

$(document).ready(function() {
    administration.init();
});



var administration = {
    ...


   init : function() {

      try {

          $.getScript("scripts/datagrid.js", function(){
              alert("Script loaded but not necessarily executed.");

            });


       } catch (e) {
        alert('[administration.init] error = ' + e);
       }


   ...

I have validated that JQuery is loaded already by checking the value of an element that is hard-coded in HTML using JQuery. Which is also validated by the fact the script is executed.

The script file exists and works if I add it to the HTML header, but I want to load it dynamically.

Why does the anonymous function never fire (obviously because the script never loads - why)?

EDIT

Following Sanjay's advice below, I bound the error handler to the ajax call and the console now displays the following:

TypeError: Cannot read property 'init' of undefined(…)

So it's finding the file, just not apparently able to find the init function of the class.

19
  • Do you get any errors in your JavaScript console? Do you see the request made for the js file? (maybe it returns a 404 or something, or the wrong mime-type). Commented Mar 8, 2016 at 13:39
  • 1
    @Neil should the console have a 404 not found error then? Commented Mar 8, 2016 at 13:44
  • 1
    $.getScript is just a shortcut for $.ajax (api.jquery.com/jquery.getscript). Could you use the long form and add an error handler? Commented Mar 8, 2016 at 13:45
  • 1
    @Neil An ajax request still throw a 404 in the console even if there is no failure callback. Commented Mar 8, 2016 at 13:46
  • 1
    @Neil Fully qualifying throws a 404 not found error on the file reference. Commented Mar 8, 2016 at 13:47

2 Answers 2

1

you should try to load script like

$.getScript( "ajax/test.js" )
  .done(function( script, textStatus ) {
    console.log( textStatus );
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log( exception );  // here you can get reason why file is not loading 
});

you can see console error Not Found here jsfiddl

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. But I want to know why this extremely popular post and answer are not working.
@AndroidAddict Because you are doing something wrong. You have to debug it, nothing we can help you
@AndroidAddict what's error are you getting? May be $.getScript() is not getting correct script path
I don't see any errors in the console nor are any raised in the try block the script is executed in.
@AndroidAddict done or fail method is executing of getScript? you should get something on console either txtStatus or exception.
1

Change code using the sequence like this: (remove try/hide and use event handler to debug this) call it AFTER you declare it.

if(!jQuery){alert("No jQuery loaded")}
var administration = {
  init : function() {
    $.getScript( "scripts/datagrid.js" )
    .done(function( script, textStatus ) { 
        console.log( textStatus );
    })
    .fail(function( jqxhr, settings, exception ) { 
        alert("exception:"+ exception ); 
    });
  }
};
$(document).ready(function() {
    administration.init();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.