5

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:

<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'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);
Joe Garrick
  • 59
  • 1
  • 1
  • 3
  • 11
    First, try correcting your quotes. It should be `"script.js"`. Next, I'd suggest you open up your console (hit F12). This will be really helpful as you go along because it will show you error messages when things go wrong. Those messages are what everyone uses to determine why something isn't working. – Mike Cluck Mar 28 '16 at 19:21
  • Probably you will need the document.ready function to cover all of your function – Luan Soares Mar 28 '16 at 19:31
  • 3
    @LuanSoares No, completely false. A) They're not using jQuery. B) They aren't accessing the DOM. C) Even if they were accessing the DOM, their script is loading after all of the DOM is loaded. – Mike Cluck Mar 28 '16 at 19:31
  • @MikeC you are rigth, they aren't accessing the DOM, my bad – Luan Soares Mar 28 '16 at 19:35
  • Be aware there is a global [`name`](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) property which has a getter and setter. Use a closure to avoid conflicts with that. – Oriol Mar 28 '16 at 19:57

3 Answers3

6

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

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

Instead of this:

<script src=“script.js”></script>

Let me know if it works.

user326964
  • 452
  • 1
  • 5
  • 13
  • Worked! Thank you so much! I'm using Sublime as my text editor. Surprised they haven't accounted for smart quotes. On my Mac I went System Preferences > Keyboard > Text, then deselected "Use Smart Quotes and Dashes." – Joe Garrick Mar 28 '16 at 20:31
  • Wow...I has the same problem and this answer solved it! Could never think that the problem lied on the quotes, jesus.. – arxakoulini Jun 23 '18 at 17:03
1

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

0

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

<html>
    <head>
        <title>Simple Page</title>
    </head>
    <body>
        <p>This is a very simple HTML page</p>
                <script src="script.js"></script>
    </body>
</html>
raddevus
  • 8,142
  • 7
  • 66
  • 87