Betzyc Betzyc - 2 months ago 10
HTML Question

script.js file not linking to index.html

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 Source

To use this you must include both jQuery and surround the javaScript with tags. If, however, you want to include everything in script.js, it might not be the best thing to write everything in tags, just write everything in "script.js"

index.html

<head>
<script src="jquery-1.10.2.min.js"></script>
<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> 

script.js

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

This should do the trick. (also remember that if you use this kind of import, you have to place script.js in the same folder. If you placed it in a different folder such as 'js' you have to do a import like:

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