从一个需要另一个JavaScript文件的JavaScript文件调用函数

时间:2022-03-17 15:59:15

I am trying to call a function written in one JavaScript file from another JavaScript file. I have the following code, but it doesn't work:

我试图从另一个JavaScript文件调用一个JavaScript文件中编写的函数。我有以下代码,但它不起作用:

My HTML file

<script type="text/javascript" src="js1.js"></script>
<script type="text/javascript" src="js2.js"></script>
<script language="javascript">
    js1();
</script>

js1.js

function js1()
{
    alert("Hello from js1");
    js2();
}

js2.js

function js2() 
{
    alert("Hello from js2");
}

What can I do?

我能做什么?

2 个解决方案

#1


6  

Try changing the order

尝试更改订单

<script type="text/javascript" src="js2.js"></script>
<script type="text/javascript" src="js1.js"></script>
<script language="javascript">
   js1();
</script>

Because you call js2(); inside js1.js, so the script js2.js should be executed before.

因为你调用js2();在js1.js里面,所以脚本js2.js应该在之前执行。

In your case, i think it should still work without changing orders like this because you call js2(); inside a function. When this script is executed:

在你的情况下,我认为它应该仍然可以工作而不改变这样的订单因为你调用js2();在一个函数内部。执行此脚本时:

function js1()
{
   alert("Hello from js1");
   js2();
}

Even the js2.js is not executed yet, but you do not actually call js2(); at this time.

即使是js2.js还没有执行,但你实际上并没有调用js2();此时。

Just try it to see if it works.

试试它是否有效。

#2


2  

I'm going to assume that's your entire HTML page.

我将假设这是你的整个HTML页面。

In order to have those scripts run, you need to have those JavaScript files in the same folder as your webpage, and to actually have a proper HTML page!

为了运行这些脚本,您需要将这些JavaScript文件放在与您的网页相同的文件夹中,并实际拥有一个合适的HTML页面!

In your HTML page, you need to include the references to your js1 and js2 files in either the head or body, and include the script you've written in the HTML page itself in the body so that it'll execute when it's loaded:

在HTML页面中,您需要在头部或正文中包含对js1和js2文件的引用,并将您在HTML页面本身中编写的脚本包含在正文中,以便在加载时执行:

<!DOCTYPE html>
<!-- ^ Declaring this DOCTYPE means this is a HTML5 page. -->
<html>
    <head>
        <!-- This will load your scripts into the document. -->
        <script src="js1.js"></script>
        <script src="js2.js"></script>
        <!--
            In a HTML5 page, you don't need to include the
            'type="text/javascript"' attribute on script tags.
            They're treated as having that by default, unless you say otherwise.
        -->
    </head>
    <body>
        <!--
            You could also include your scripts here, but I'll
            just leave these commented out since they're already included.
        <script src="js1.js"></script>
        <script src="js2.js"></script>
        -->
        <script>
        js1();
        </script>
        <!--
            You don't need 'language="javascript"' on a script tag.
            Use the type attribute, or nothing in a HTML5 page.
        -->
    </body>
</html>

#1


6  

Try changing the order

尝试更改订单

<script type="text/javascript" src="js2.js"></script>
<script type="text/javascript" src="js1.js"></script>
<script language="javascript">
   js1();
</script>

Because you call js2(); inside js1.js, so the script js2.js should be executed before.

因为你调用js2();在js1.js里面,所以脚本js2.js应该在之前执行。

In your case, i think it should still work without changing orders like this because you call js2(); inside a function. When this script is executed:

在你的情况下,我认为它应该仍然可以工作而不改变这样的订单因为你调用js2();在一个函数内部。执行此脚本时:

function js1()
{
   alert("Hello from js1");
   js2();
}

Even the js2.js is not executed yet, but you do not actually call js2(); at this time.

即使是js2.js还没有执行,但你实际上并没有调用js2();此时。

Just try it to see if it works.

试试它是否有效。

#2


2  

I'm going to assume that's your entire HTML page.

我将假设这是你的整个HTML页面。

In order to have those scripts run, you need to have those JavaScript files in the same folder as your webpage, and to actually have a proper HTML page!

为了运行这些脚本,您需要将这些JavaScript文件放在与您的网页相同的文件夹中,并实际拥有一个合适的HTML页面!

In your HTML page, you need to include the references to your js1 and js2 files in either the head or body, and include the script you've written in the HTML page itself in the body so that it'll execute when it's loaded:

在HTML页面中,您需要在头部或正文中包含对js1和js2文件的引用,并将您在HTML页面本身中编写的脚本包含在正文中,以便在加载时执行:

<!DOCTYPE html>
<!-- ^ Declaring this DOCTYPE means this is a HTML5 page. -->
<html>
    <head>
        <!-- This will load your scripts into the document. -->
        <script src="js1.js"></script>
        <script src="js2.js"></script>
        <!--
            In a HTML5 page, you don't need to include the
            'type="text/javascript"' attribute on script tags.
            They're treated as having that by default, unless you say otherwise.
        -->
    </head>
    <body>
        <!--
            You could also include your scripts here, but I'll
            just leave these commented out since they're already included.
        <script src="js1.js"></script>
        <script src="js2.js"></script>
        -->
        <script>
        js1();
        </script>
        <!--
            You don't need 'language="javascript"' on a script tag.
            Use the type attribute, or nothing in a HTML5 page.
        -->
    </body>
</html>