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?

icedwater
  • 4,701
  • 3
  • 35
  • 50
AloNE
  • 311
  • 3
  • 7
  • 15
  • 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 Jul 05 '13 at 04:34
  • Based on the above order, You can call js1 function in js2 or the page. – Deepu Madhusoodanan Jul 05 '13 at 04:35
  • 1
    Is that your _entire_ HTML file? If so, is it in the same folder as the JavaScript files? – doppelgreener Jul 05 '13 at 04:47
  • the `script` `language` attribute is deprecated. Use `type` or omit both (valid in HTML5). See http://stackoverflow.com/questions/2267476/html-script-tag-type-or-language-or-omit-both – John Dvorak Jul 05 '13 at 04:52

2 Answers2

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.

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • 1
    "because you call js2(); inside a function" is not a sufficient condition. Rather, "... function that is run after `fn2` gets defined – John Dvorak Jul 05 '13 at 04:46
  • 1
    @Jan Dvorak: I updated my answer. I guess there is another problem with the OP's code that is not shown in the question. – Khanh TO Jul 05 '13 at 04:53
  • 1
    Yep. If the files are loaded, asker's code should work just fine. As far as I can tell, your suggestion won't help. – John Dvorak Jul 05 '13 at 04:56
  • @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. – Khanh TO Jul 05 '13 at 04:57
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>
doppelgreener
  • 4,809
  • 10
  • 46
  • 63