9

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?

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. Commented Jul 5, 2013 at 4:34
  • Based on the above order, You can call js1 function in js2 or the page. 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? 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/… Commented Jul 5, 2013 at 4:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

"because you call js2(); inside a function" is not a sufficient condition. Rather, "... function that is run after fn2 gets defined
@Jan Dvorak: I updated my answer. I guess there is another problem with the OP's code that is not shown in the question.
Yep. If the files are loaded, asker's code should work just fine. As far as I can tell, your suggestion won't help.
@Jan Dvorak: you're right, I just suggest him that he should take the order of scripts execution into account. This suggestion may not help in this case. But in general, he should care about it.
3

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.