0

I have a .js file defined as follows:

<script>
    function sayHello() {
        alert("Hello World");
    }
</script>

And I have a .html file defined as follows:

<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="css/testCSS.css">
        <title>test html</title>
    </head>
    <body>
        <script type="text/javascript" src="js/testJS.js"></script>
    </body>
</html>

How can I call the sayHello() function?

1
  • The .js file should not have any script tags Commented May 11, 2014 at 5:32

3 Answers 3

1

script.js

function sayHello() {
    alert("Hello World");
}

index.html

<!doctype html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script src="script.js"></script>
        <script>
            sayHello();
        </script>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to call it!

let me explain in an example. let's say you have a button in that html that calls sayHello function.

<button type="button" onclick="sayHello ()">Say hello</button>

I think you should take a look at here.

Comments

0

Jus replace the link to script with content of .Js file...

Or call the function

     <script>
       sayHello()
     </script>

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.