-3

I keep getting problems running code on localhost in any browser. When I test my code in JSfiddle it works fine. In my browser CSS loads and even jQuery library loads but my external vanilla javascript files never work. I get this error in the console:

script.js:1 Uncaught ReferenceError: $ is not defined
    at file:///C:/Users/Alex/Desktop/myfile/script.js:1:1

I'm pretty sure there are no errors linking to my js files. Here is how I am linking to my files:

<html>
<head>
  <meta charset="utf-8">
  <title>TitleofProject</title>
  <link type="text/css" rel="stylesheet" href="style.css"/>
  <script type="text/javascript" src="script.js"></script>
</head>

I tried wrapping my external javascript files in:

$(window).load(function() {
}};

but this does not work either.

Javascript is allowed in my browser settings.

moopet
  • 6,014
  • 1
  • 29
  • 36
  • 4
    `$` isn't built-in¹. You need to add a script that defines it. One such script is jQuery. (¹ Except in the browser console, in some browsers. But only in the console, not the page itself.) – T.J. Crowder Nov 23 '16 at 16:46
  • 4
    You haven't added jQuery library before the `scripts.js`. You can do so by adding this line: ``. – Praveen Kumar Purushothaman Nov 23 '16 at 16:46
  • 2
    Stop downvoting a valid question. – Andy Ray Nov 23 '16 at 16:48
  • 1
    Please [search](/search?q=%5Bjs%5D+%24+is+not+defined) before posting, this has been asked and answered dozens of times, for instance [here](http://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined), and [here](http://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined). – T.J. Crowder Nov 23 '16 at 16:49
  • 2
    @AndyRay: People are expected and required to make a solid effort to find previous answers. In this case, just searching the error message turns up plenty of them. "Shows no research effort" is perfectly valid. – T.J. Crowder Nov 23 '16 at 16:51
  • 1
    @AndyRay I didn't downvote, but upvotes/downvotes don't have anything to do with whether a question is valid. If it's valid, it shouldn't be closed, but a valid question can still be an appropriate target for downvotes if it shows little or no research, is missing key data, etc.. – Beofett Nov 23 '16 at 16:52
  • Hi, adding the Jquery library before the script tags has no impact. My question has nothing to do with JQuery. I can get Jquery to work fine. Im referring to linked vanilla javascript files only. Sorry if you misunderstand what i mean im very new to the industry. This is also my first post on Stack Overflow. Apologies again. I have tried to seek an answer but all seem to refer to Jquery which my question is not about. – Alexander Johnston Nov 24 '16 at 03:14

2 Answers2

2

$ refers to jQuery. You need to include jQuery in your sources.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
user835611
  • 2,309
  • 2
  • 21
  • 29
  • 1
    *"$ refers to jQuery."* Not necessarily. It refers to whatever it's set up to refer to. If you're using jQuery, that could be jQuery. If you're using MooTools, that could be MooTools. If PrototypeJS, it could be PrototypeJS. Or you could set it up to be your own function, perhaps a wrapper for `document.querySelectorAll` (which I've seen people do). – T.J. Crowder Nov 23 '16 at 17:04
  • 2
    @T.J.Crowder Your comment is correct, but it's worth noting that the question explicitly refers to jQuery. – elixenide Nov 24 '16 at 01:57
1

You need to add jQuery to your html page before your script. Only then you can use the jQuery functions ($):

 <script src="https://code.jquery.com/jquery-3.1.1.min.js"
         integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
         crossorigin="anonymous"></script>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Sven
  • 2,839
  • 7
  • 33
  • 53