0

I just got into javascript today but I can't figure out how to load it into my website. I've been looking everywhere for a solution but nothing seems to help.

This is my body in index.html

<body>
<p id="testid">TEST</p>
<script type="text/javascript" src="./script.js"></script>
</body>

This is some example java script code I grabbed off the internet, I tried other examples as well but I can't get anything to work.

<script type="text/javascript">
var para = document.createElement("p");
var node = document.createTextNode("This is new.");
para.appendChild(node);

var element = document.getElementById("testid");
element.appendChild(para);
</script>

The html and javascript file are both in the same folder, I'm clueless as to why this won't work... I appreciate any help :)

enter image description here

4
  • 1
    Try changing the src to "script.js" in the script tag. jsfiddle.net/#&togetherjs=ehlcmR9J8M Commented Apr 27, 2016 at 13:17
  • There should be something interesting in the JavaScript console. Commented Apr 27, 2016 at 13:23
  • What are you trying to accomplish? There's no event on the HTML to call the JavaScript, and there's no named function on the JavaScript code. Example, if you're trying to call the function on the <body> tag on form load, it should be <body onload="JavaScriptFunctionName();">, and that will load the JavaScript function. And on JavaScript, you would need to create a function named function JavaScriptFunctionName(){} where you will do the logic. Commented Apr 27, 2016 at 13:30
  • @Auguste: It does not need to be wrapped, it will execute as soon as it is included. He included the script at the end of the body tag, so it will load once the rest of the body has loaded (excluding images). However, it should probably be wrapped in a self-executing function (e.g. (function() { /* do something */})();) to avoid polluting the global scope. Commented Apr 27, 2016 at 13:35

1 Answer 1

12

Omit the <script></script> tags in the .js file.

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

1 Comment

Thank you so much! I'll mark this as answer once I'm allowed to (15 min)

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.