最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Calling a function from one JavaScript file which requires another - Stack Overflow

programmeradmin2浏览0评论

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:

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?

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:

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?

Share Improve this question edited Jul 5, 2013 at 4:52 icedwater 4,8873 gold badges38 silver badges53 bronze badges asked Jul 5, 2013 at 4:33 AloNEAloNE 3113 gold badges7 silver badges15 bronze badges 4
  • Yes, you absolutely can, but you have to make sure that you include the files in the right order so that the function code has already been loaded into your document before you try to call the function. – HartleySan Commented Jul 5, 2013 at 4:34
  • Based on the above order, You can call js1 function in js2 or the page. – Deepu Madhusoodanan Commented Jul 5, 2013 at 4:35
  • 1 Is that your entire HTML file? If so, is it in the same folder as the JavaScript files? – doppelgreener Commented Jul 5, 2013 at 4:47
  • the script language attribute is deprecated. Use type or omit both (valid in HTML5). See stackoverflow.com/questions/2267476/… – John Dvorak Commented Jul 5, 2013 at 4:52
Add a comment  | 

2 Answers 2

Reset to default 7

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.

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:

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.

Just try it to see if it works.

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

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!

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:

<!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>
发布评论

评论列表(0)

  1. 暂无评论