0

Trying to learn how libraries work in HTML.

In my HTML file, I am trying to make use of functions from a JavaScript file that is located in the same folder as the HTML file. I would like the function to tell the HTML to print a simple message to the screen, so I know I've made some progress, but alas, I have no clue what I'm doing. This is as far as I got with online sources and a textbook I have on hand.

Here's what my HTML looks like so far:

<!doctype html>
<!-- project9.html -->

<html>
    <head>
        <title>Testing Function</title>
        <script type="text/javascript" src="test.js"></script>
        <script type="text/javascript">
            function Test()
        </script>
    </head>
    </body>
</html>

This is the test.js file, located in the same folder as the HTML file:

function Test(){
    return ' test '
}

I'm overlooking something super simple, aren't I?

1
  • remove function before your call to Test() Commented Nov 13, 2020 at 18:51

2 Answers 2

4

When you want to CALL a function, you just need to refer to that function's name (and any arguments but in this case that isn't relevant) and not the word function.

Also you are RETURNING the string, not doing anything with it. This will at least just alert it.

function Test(){
    return ' test '
}

alert(Test())

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

9 Comments

Some people like to use semi-colons in JS but it isn't necessary
@berkobienb, I have a javacript minifier that I use, so I prefer to use them. But you are correct they aren't needed to terminate lines.
You could also just use console.log(Test()) rather than alert()
@berkobienb, yes but if the OP is new I didn't want them to implement the code and have no idea why they can't see anything.
@ConnerMcClellan, your browser has a web developer/console area. I believe some times it is accessible via the F12 button, otherwise its accessible in one of the browser's drop down menus.
|
1

To modify HTML content on the screen, you'd use get a reference to the element and then use innerHTML

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Test</title>
  </head>
  <body>
    <p id="test"></p>
    <script src="./index.js"/> <!-- just import the file -->
  </body>
</html>

index.js

function test() {
  var p = document.getElementById("test");
  p.innerHTML = "test";
}
test(); //notice the function call

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.