-1

I have the following test.html file and from it I'm trying to call sayHello() from the myjslib.js file. sayHello simply prints "Hello!" to the console but nothing is printing. How do I call sayHello from the test.html file? This question may seem like a duplicate of this question. But, that question is addressing the use of script code inside a script tag that has a src attribute. I now have the function call in a separate script tag yet they still does not launch.

HTML body

<body>
    <script src="myjslib.js"></script>
    <script>
        sayHello();
    </script>
</body>

These are the functions in the JavaScript file

function sayHello() {
    console.log("Hello!");
}

function sayGoodbye() {
    console.log("Goodbye!");
}
1
  • Is there anything in the console? Any errors? Commented Oct 19, 2017 at 10:29

1 Answer 1

1

You need to refactor your code like this:

<script src="myjslib.js"></script>
<script>        
  sayHello();
</script>

See documentation about src attribute

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

3 Comments

Thanks for responding Flying but after refactoring I now get "Uncaught ReferenceError: sayHello is not defined"
I edited the above to reflect what I now have after refactoring.
Thanks Flying. I probably didn't save my changes but now it works as you suggested.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.