Reputation: 27
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?
Upvotes: 0
Views: 48
Reputation: 15847
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())
Upvotes: 4
Reputation: 131
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
Upvotes: 1