Search code examples
javascriptjqueryuncaught-exception

Uncaught ReferenceError: $ is not defined error received on simple web page


I am writing a simple webpage. I included jQuery and under the include, I alert a message but it dosn't show anything. I get uncaught reference error in the console.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Testing Media Queries</title>
    <script type="text/javascript" href="jquery-1.11.3.min.js"></script>
    <script>
      $( document ).ready(function() {
        console.log( "ready!" );
        alert('hello');
      });
    </script>
  </head>
  ...

Solution

  • If you are trying to include a local file and the project's structure is like this:

    |- test.html
    |- jquery.js
    

    Then in the header, the following will work (notice the use of src instead of href)

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

    For more information about href vs src, see this.

    If you don't want to host the file on your server, then consider importing it from the CDN like this:

    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>