Javascript will not link to HTML

Disclaimer: I programmed all fifteen minutes. I am trying to follow in the Lynda.com class of Programming Foundations class, but I cannot link my JS with my HTML. They are in the same folder. When I open the HTML file in my browser, I don't get the JS popup that the dude does in the video. What am I doing wrong?

Here is my HTML file:

<html> <head> <title>Simple Page</title> </head> <body> <p>This is a very simple HTML page</p> <script src="script.js"></script> </body> </html> 

And here is the JS file called "script.js" in the same folder as the HTML file.

 var name = prompt("What is your name?"); alert("Hello, " + name); 
+5
source share
4 answers

This worked for me when I changed the quotes to straight lines. Copy and paste this:

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

Instead of this:

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

Let me know if it works.

+4
source

To correct your quotes, use no. The browser cannot parse your html. Use the developer console to check for errors in the future.

+1
source

Try this plunker. It has your code exactly (even one script.js name), except that I have changed the quotation marks, and it works fine. Use Plunker to test all your js.

https://plnkr.co/edit/4Mw0RmWfblGp9U8gsuvx

 <html> <head> <title>Simple Page</title> </head> <body> <p>This is a very simple HTML page</p> <script src="script.js"></script> </body> </html> 
0
source

Links to external JavaScript files should be in the head section of your page, for example:

 <html> <head> <title>Simple Page</title> <script src="script.js"></script> </head> <body> <p>This is a very simple HTML page</p> </body> </html> 

And the file path should be relative to the location of the HTML file. So, if you have HTML and a .js file in the same folder, then the file name is correct.

Make sure both files are saved and that the .js file name is exactly what you have in the HTML file. Also, use the direct quotation marks (") that you get with a text editor, not the formatted quotes (" ") that you get with applications like Word.

-1
source

Source: https://habr.com/ru/post/1245933/


All Articles