Uncaught TypeError: Cannot read property 'defaults' of undefined

Uncaught TypeError: Cannot read property 'defaults' of undefined

nachitoxnachitox Posts: 2Questions: 1Answers: 0

Hi,

This is the code with the issue:

$.getScript(config.urlDataTablesJS, function() {
    $.extend($.fn.dataTable.defaults, {
        // etc
    });
    // etc
});

where config.urlDataTablesJS = https://static2.sitejabber.com/js/datatables.min.1476819401.js
The file exists and the request is finish before running $.extend.

I wonder why $.fn.dataTable is undefined. I even try with a timeout but the same occurs.
Any idea why?

Thanks

Answers

  • nachitoxnachitox Posts: 2Questions: 1Answers: 0

    anyone ?

  • rf1234rf1234 Posts: 2,987Questions: 87Answers: 421

    Found this here: https://api.jquery.com/jquery.getscript/

    "Success Callback
    The callback is fired once the script has been loaded but not necessarily executed."

    Looks like the missing execution of this file is the issue: https://static2.sitejabber.com/js/datatables.min.1476819401.js

    Just take a look at this - or you might find something better elsewhere:
    https://stackoverflow.com/questions/14565365/jquery-getscript-callback-when-script-is-fully-loaded-and-executed

  • rf1234rf1234 Posts: 2,987Questions: 87Answers: 421

    This seems like the best solution - I would try this one:

    "The callback is fired once the script has been loaded but not necessarily executed.
    what you need to look for instead is how promises act. In this case as you are looking for a script to load asynchronously you can use either "then" or "done" have a look at this thread which explains the difference nicely: https://stackoverflow.com/questions/5436327/jquery-deferreds-and-promises-then-vs-done

    Basically done will be called only when a promise is resolved. in our case when the script was loaded successfully and finished running. So basically in your code it would mean:

    $.getScript("test.js", function() {
        foo();
    });
    

    should be changed to:

    $.getScript("test.js").done(function(script, textStatus) {
        console.log("finished loading and running test.js. with a status of" + textStatus);
    });
    

    "

  • allanallan Posts: 63,480Questions: 1Answers: 10,467 Site admin

    My guess is that jQuery is being loaded multiple times. DataTables might be getting attached to the first, and then when jQuery is loaded again it will overwrite the first and take any plug-ins with it (that's just how jQuery works).

    Might that be the case here?

    Allan

This discussion has been closed.