0

There are many questions similar to this but none of them have been able to help me.

I am new to HTML and JS development. I have a simple HTML file that is trying to access a simple Javascript file.

HTML:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
document.write("In HTML")

</script>

<script type="text/javascript" src=./asdf.js></script>
</head>
<body>
<div id="map3d" style="height:400px;width:600px;border:1px solid red"></div>
</body>

The Javascript file is named asdf and exists in the same location as the HTML. I have tried the full file path, just the name "asdf.js" and several other options I can't recall.

My .js file reads thusly:

<script>
document.write("In JS")
</script>

The "In JS" doesn't appear. What am I doing wrong?

2 Answers 2

3

External scripts do not require <script> and </script>. Write external scripts as if you were writing in between the tags.

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

5 Comments

Okay, thanks. But there is apparently still another problem because "In JS" still doesn't display.
It actually might be displaying. You are executing your JavaScript in the <head> block and not the <body>
@KyleWright you can check for errors using the console (F12 or CTRL+SHIFT+I in most browsers)
Adding it to the <body> didn't seem to help. Pressing F12 suggested to me that there were no errors.
Never mind, you guys steered me right. When I made your changes I also changed something else, which turned out to be a mistake.
3

In your JS file remove the <script> and </script> tags. They are for using JavaScript embedded within HTML, not for external file.

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.