1

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 Answer 1

3

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();
});
Sign up to request clarification or add additional context in comments.

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.