So simple, but it's not working

So simple, but it's not working
0

#1

So this is so simple and it was working before. I’m trying to load a JS file into my html. I think I have the file extension correct, but it’s not loading the JS on my page. This is for a JS video I’m following on YouTube.

42 PM


#2

the file:// only works for local file systems, i.e. on your own computer.

If this is hosted on the Internet (codepen, your own website, etc), either use relative paths src="/directory/file.js"
or the complete url src=“http://www.googlecdn.com/folder/file.js


#3

Should it be 3 forward slashes or 2 forward slashes or 1 forward slash at the beginning?


#4

Thanks. This isn’t hosted on any other website. Just my local computer.

I tried it with 2 and 1 slashes and it still didn’t work.


#5

Where is this file in relation to the html file? Is it in the same directory? What is the actual file path for both files (.html and .js)? Is this on Windows or Mac?


#6

Use a relative path instead of an absolute path

https://www.w3schools.com/html/html_filepaths.asp


#7

This file is in the same folder as the HTML file. This is on a Mac.

The file path is /Users/darrinmaier/Desktop/javascriptBasics/index.html

and

/Users/darrinmaier/Desktop/javascriptBasics/js.js


#8

If they are in the same folder, then the following should work. Let us know.

<html>

<head>
</head>

<body>
  <h1>Title goes here</h1>
  <p>
    paragraph here!
  </p>

  <script src="js.js"></script>
</body>

</html>

#9

Actually, after reviewing your original screenshot, if you both of the files are located in a folder with the following path:

/Users/darrinmaier/Desktop/javascriptBasics

then your script src attribute was wrong, because you referenced a folder called “Javascript_Basics”, which is not the same as “javascriptBasics”.


#10

Hey Thanks for the response. It did not work.

If it helps, I’m using Atom as an editor. In my version of Atom, if I type in a link or file path it turns blue. It won’t turn blue unless I start local paths with “file:///”.

Even correcting the underscore and the uppercase J didn’t work.


#11

I got it. It wasn’t saving the index properly. Thanks so much all!


#12

I use Atom also, but this has nothing to do with Atom. It has to do with proper syntax. If you open this file outside of Atom in a web browser, it will work. What code is in js.js? If you put

alert("The JS file is working...");

inside the js.js file, and open the html file (my code above), it will display an alert.