我如何 link 一个 Javascript 文件到另一个 Javascript 文件
How do I link a Javascript file into another Javascript file
同一个文件夹中有两个文件。
我正在尝试在 app2
.
中调用名为 app1
的函数
app1.html
<script>
function greet() {
alert('hello from App1')
}
// greet() // commented out code
</script>
app2.html
<script type="text/javascript" src="./app1.html"></script>
<script>
function app2() {
alert('app2')
}
app2()
greet() // this line of code is not working
</script>
我建议使用单独的外部 js 文件而不是嵌入的 <script>
标记,但这也许会有所帮助
How to include js file in another js file?
如果您想在另一个 js
文件中调用该文件,则必须在调用文件中引用该文件。
例如
参考 head
部分中的文件。
<head>
<script src="File2.js" type="text/javascript"></script>
<script src="File1.js" type="text/javascript"></script>
</head>
您的 body
标签可以是这样的:
<body>
<script type="text/javascript">
Method2(); // Where you want to call method from another JS.
</script>
</body>
然后,使用第一个文件
File1.js
function Method1(number) {
alert("Method1");
}
File2.js
function Method2() {
Method1("Method1 is called in Method2");
}
同一个文件夹中有两个文件。
我正在尝试在 app2
.
app1
的函数
app1.html
<script>
function greet() {
alert('hello from App1')
}
// greet() // commented out code
</script>
app2.html
<script type="text/javascript" src="./app1.html"></script>
<script>
function app2() {
alert('app2')
}
app2()
greet() // this line of code is not working
</script>
我建议使用单独的外部 js 文件而不是嵌入的 <script>
标记,但这也许会有所帮助
How to include js file in another js file?
如果您想在另一个 js
文件中调用该文件,则必须在调用文件中引用该文件。
例如
参考 head
部分中的文件。
<head>
<script src="File2.js" type="text/javascript"></script>
<script src="File1.js" type="text/javascript"></script>
</head>
您的 body
标签可以是这样的:
<body>
<script type="text/javascript">
Method2(); // Where you want to call method from another JS.
</script>
</body>
然后,使用第一个文件
File1.js
function Method1(number) {
alert("Method1");
}
File2.js
function Method2() {
Method1("Method1 is called in Method2");
}