Reputation: 1432
I'm using $.getScript to load the script files, this works fine. if I check the network tab in my browser, the file did download successfully.However, when I try and use functions in the script file they are undefined. There also is no new script tag in the HTML.
How do I use the new script file once it has been downloaded?
Note: The file also does not show up in the resources tab of the dev tools after it has been downloaded.
Edit: added code below and some more info
Script being loaded file:
var STUD = function () {
return {
STUDLoaded: function () {
alert('Working!');
}
}
}
function foo()
{
alert('bar!');
}
js downloading the script file and calling the functions.
$.getScript(gSiteRoot + 'Scripts/Views/' + ControllerName + '.js')
.done(function (script, textStatus) {
debugger;
foo();
if ($('#hdnJSClass') != undefined)
window[$('#hdnJSClass').val()][$('#hdnJSClassFunc').val()]();
})
.fail(function (jqxhr, settings, exception) {
GEN._displayNotification('Error', 'There was an error loading the page, please retry', true);
});
The function foo fires correctly when called after the async loading. So the issue is with the window call, this call works for my other js functions, but not the dynamically loaded ones.
Upvotes: 2
Views: 163
Reputation: 1432
I found the issue, there was a syntax error in the STUD object with invalidated its content. I forgot to add the closing parenthesis:
var STUD = function () {
return {
STUDLoaded: function () {
alert('Working!');
}
}
}(); // <-- forgot those
Upvotes: 0
Reputation: 4288
Use .getScript()
so:
$.getScript('path/to/file.js', function(data, ts, jqxhr){
if( jqxhr.status == 200 ){
// do stuff
}
else{
// failed retrieving file
}
});
Upvotes: 1