Uncaught ReferenceError: script is not defined

7k views Asked by At

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.jsscript from an index.html page, I am getting the following error in the console:

Uncaught ReferenceError: script is not defined

1

There are 1 answers

0
Arun P Johny On BEST ANSWER

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();
});