Forum Moderators: open
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<script>
// externally defined function
function pageFx(selector, settings) {
alert(typeof(settings.callback) + ": " + settings.callback);
if (typeof(settings.callback) === "string") {
eval(settings.callback + "()");
}
else {
settings.callback();
}
}
var do_func2 = "do_func2_";
var URL = "example_com";
function do_func2_example_com() {
alert('in callback');
}
function do_func() {
pageFx(
"#ID",
{
setting: 'value',
//callback: do_func2 + URL
callback: do_func2_example_com
}
);
}
do_func();
</script>
</body>
</html>
I tried adding quotes around "do_func2_" but that doesn't work either.
alert(typeof(settings.callback) + ": " + settings.callback)
I don't really want to run it via alert(). Is there an easier way?
It runs perfectly if I hard-code the function name, like so:
function do_func() { pageFx("#ID", {setting: 'value', callback: do_func2_URLpage }); }
function do_func2_URLpage() {
//...
}
<script src="file1.js"></script><!-- can't call methods in file2.js yet -->
<script src="file2.js"></script>
<!-- now you have access to both files -->
<!DOCTYPE...>
<html...>
<head>
...
<script type="text/javascript" src="external_file.js"></script>
<script type="text/javascript">
function func1(){..., callback: func2;}
function func3(){...}
</script>
[external_file.js]
function func2(){..., callback: func3;}
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<script src="external_file.js"></script>
<script>
function func1() {
func2();
}
function func3() {
alert('in func3');
}
func1();
</script>
</body>
</html>
// external
function func2() {
func3();
}