I’m trying to learn jQuery and I don’t understand why it doesn’t at all work. My index.html is simple, as you can see below:
<!DOCTYPE html>
<html>
<head>
<title>JQuery Tutorial</title>
</head>
<body>
$(function() { alert("Hello world!"); });
<script src="js/jquery-1.8.0.min.js"></script>
</body>
</html>
I am hosting the website with XAMPP in my local machine, and the file structure is this, under htdocs:
XAMPP/xamppfiles/htdocs
...
jquerytuts2/
index.html
/js
jquery-1.8.0.js
jquery-1.8.0.min.js
The jQuery just displays as it is in the browser, plain text. I’ve seen this on Chrome, Safari, Firefox, and I am using a 2012 MacBook Pro running OSX 10.8.1.
Explanation: Because the document usually gets executed from top to bottom, you execute jQuery code before jQuery itself is loaded, causing the site to blow up. Oh yeah, and you forgot to wrap a
<script>tag around your JavaScript code.Edit: And some further advice: Unless you have a reason not to do so, place your javascript below anything else in the site. That causes the site to load faster. There are HTML5 attributes for script tags that make this unneccessary (in theory).