script.js file not linking to index.html

Betzyc picture Betzyc · Mar 23, 2014 · Viewed 50k times · Source

I am using sublime and trying to practice building a site. Here's the HTML and JS that I am trying to connect, please tell me what I'm doing wrong because the alert is not coming up when I open index.html in Chrome.

 <!DOCTYPE html>
<head>
<link href='http://fonts.googleapis.com/css?family=Quicksand:300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="script.js"></script>
</head>

    $(document).ready(function(){
alert("Hello World");

});

Thank you!

Answer

Sarfraz picture Sarfraz · Mar 23, 2014

You need to put javascript code in <script></script> tags:

<!DOCTYPE html>
<head>
<link href='http://fonts.googleapis.com/css?family=Quicksand:300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="script.js"></script>
<script>
    $(document).ready(function(){
      alert("Hello World");
    });
</script>
</head>

And since you are using ready method of jQuery, make sure it is included first before using it with:

<script type="text/javascript" src="jquery.js"></script>