在脚本标记块之间调用JavaScript函数

gsu*_*nic 0 javascript

我有两组<script>包含JavaScript函数的标记块,并将它们置于优先级顺序中.其中一个标记包含src另一个外部.js库文件,如下所示.

<script src='libtest.js'>
    function helloworld() {
        alert('hello world');
    }

    function callLibraryTest() {
        runLibraryTest(); //Calls into libtest.js for auto test.
    }
</script>

... some html ...

<script>
    function callHello() {
        helloworld();
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我得到的错误是callHello()函数没有helloworld()定义.我该如何解决?

请注意,脚本是故意分开的,因为如果它们聚集在一起,那么调用callHello()might最终也不会被定义.

谢谢.

Dan*_*owe 7

如果<script>标记具有src属性,则它也不能包含脚本文本.将该helloworld功能移动到单独的<script>标签.

<script src="libtest.js"></script>

<script>
    function helloworld() {
        alert('hello world');
    }

    function callLibraryTest() {
        runLibraryTest(); // Calls into libtest.js for auto test.
    }
</script>

<!-- some html ... -->

<script>
    function callHello() {
        helloworld();
    }
</script>
Run Code Online (Sandbox Code Playgroud)