Can t link javascript to html

Disclaimer: I've been programming for all of fifteen minutes. I'm trying to follow along on Lynda.com's Programming Foundations class, but I can't seem to link my JS to my HTML. They're in the same folder.When I open the HTML file on my browser, I don't get the JS pop-up box that the dude in the video does. What am I doing wrong?

Here's my HTML file:


    
        Simple Page
    
    
        

This is a very simple HTML page

And here's the JS file titled "script.js" in the same folder as the HTML file.

var name = prompt("What is your name?");
alert("Hello, " + name);

asked Mar 28, 2016 at 19:20

Can t link javascript to html

5

It worked for me when I changed the quotation marks to straight ones. Copy and paste this:


Instead of this:


Let me know if it works.

answered Mar 28, 2016 at 19:44

user326964user326964

4241 gold badge5 silver badges13 bronze badges

2

Fix your quotes use " not “. The browser is unable to parse your html. Use the developer console to check for errors in the future.

answered Mar 28, 2016 at 19:44

Try this plunker. It has your code exactly (even same name script.js) except I changed the quote chars and it works perfectly. Use Plunker to test all your js.

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


    
        Simple Page
    
    
        

This is a very simple HTML page

answered Mar 28, 2016 at 19:55

Can t link javascript to html

raddevusraddevus

7,4775 gold badges61 silver badges74 bronze badges

1

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Hey forum!

I'm having a lot of problems linking my JS file to my HTML file, and I can't figure out what's wrong.

I apologize in advance if my code doesn't post right. This is my first time asking a question on this forum....

This is just an excerpt from the bottom of my HTML code where the script tag is. I've verified(about 15 times) that the .js file name is spelled correctly and in the same folder as the HTML file.

  <footer>
   © BC Webinations 2014
  footer>
  <script> src="calc.js"script>
body>
html>

My JS is just a simple alert to let me know it's working, which it isn't...

Now, I'm aware that I didn't declare a "type" in the script tags, and that's because every time I do, the javascript returns a syntax error.

I'm expecting it to be just a stupid little typo or something....

Thanks guys!

-Brent C.

P.S. I'm running the latest Google Chrome with Ubuntu, if that means anything.

7 Answers

Jeff Busch November 8, 2014 2:14am

Hi Brent,

Change:

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

To:

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

Jeff

Can t link javascript to html

STAFF

Brent;

You need to put your src assignment inside your script tag. should do the trick.

You can move it to a separate js folder like Robert mentioned, that is a pretty common practice.

Happy coding, Ken

Can t link javascript to html

STAFF

not sure, but try putting your js into it's own subfolder that sits next to your html file, name the subfolder simply js, and then you link like this,

hope that helps

Brent Cralley November 8, 2014 2:14am

Gave your suggestion a shot, Robert, and still no dice. From what you can see, all of my syntax is correct, right?

-Brent C.

Brent Cralley November 8, 2014 2:21am

Thanks for the help, guys! I knew it had to be something stupid that I just wasn't seeing. ;-)

Now, on the plus side, my script tag now accepts "type="text/javascript" without showing any syntax errors.

On the downside, it's still not showing any of my javascript in either the console or a pop-up alert window....

Brent Cralley November 8, 2014 2:36am

 <footer>
   © BC Webinations 2014
  footer>
  <script type="text/javascript" src="calc.js">script>
body>
html>

alert("Help!");
console.log("Help me!");

Jeff Busch November 8, 2014 2:47am

With the code you have the html and javascript files must be in the same directory. Your code works on my computer.

Why my JavaScript is not working in HTML?

On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.
To link a Js file with your HTML, you only have to add the source of the script inside the body tag or outside; it doesn't matter. If you are working with libraries, like jQuery, you can add the URL of the library in the source.

Why my external js is not working?

“external javascript file not working” Code Answer's Most likely, the problem is that you are including your js file in a head tag or somewhere above your main content. ... You should be able to add the js file in a script tag. The page loads first then JavaScript.

Can you use JavaScript and HTML together?

JavaScript can be used in combination with HTML to power modern web applications that are intuitive, interactive and user-friendly. By using simple client-side validation, it reduces server traffic and improves the overall efficiency of the website.