Reputation: 8357
In jQuery, how do I call a function in a script that I retrieve via code?
I have a javascript file called testjavascript.js
Here is the code:
function testfunctionwithdata(testdata)
{
alert(testdata);
}
function testfunction()
{
alert("testfunction");
}
I also have a javascript called testcalljavascriptfunction.js
Here is the code:
var scripturl = "/objects/testjavascript.js";
$.getScript(scripturl, function() {
script.testfunction();
});
When I load the testcalljavascriptfunction.js
script from an index.html
page, I am getting the following error in the console:
Uncaught ReferenceError: script is not defined
Upvotes: 1
Views: 7196
Reputation: 388406
There is no object called script
in your context, when the script file is loaded it is parsed and executed. Since your method seems to be in global context you can call it by using testfunction()
var scripturl = "/objects/testjavascript.js";
$.getScript(scripturl, function() {
testfunction();
});
Upvotes: 3