I and trying to get started with JQuery “jQuery: Novice to Ninja “ But I cannot get the program to recognize the jQuery command.
<!doctype html>
<html>
<head>
<title> JQuery_Test </title>
<script type=‘text/javascript’ src=‘jquery-1.2.6-min.js’></script>
<script type=‘text/javascript’ src=‘script.js’></script>
</head>
<body>
$(function() { alert(‘Ready to do your bidding!’); });
<script>
alert(“Hello World.”);
</script>
</body>
</html>
The Script “Hello World” works fine but the JQuery function ‘Ready to do your bidding!’ just prints the line a though it was plane text. The file ‘jquery-1.2.6-min.js’ is in the same folder as the html file “JQuery_Test.html”.
$(function() { alert('Ready to do your bidding!'); });
Put simply, this tells jQuery to run the supplied function when the document has finished loading (don’t confuse that with page, which also includes images, css etc). So jQuery ‘waits’ until the </html> tag has been loaded, then it runs the function, showing your alert.
alert("Hello World_xx");
This tells javascript to open your alert immediately, whether the document’s finished loading or not.
Thank you very much, it appears that you have found the last of several errors I made.
What a difference a little js will do for you.
I now have two pop-ups it surprised me though the “Hello World_xx” poped up first then the “Ready to do your bidding!”
I guess when I learn more about jquery I’ll understand why. I think I read somewhere that jquery doesn’t do any thing until the program is finished loading. That way the script alert is acted upon when the program load that line.
I think I know what happened: to make sure that the spelling was correct i copied and pasted but XP does not show the extension along with the file name, so when I copied I forgot to add the .js.
Nice to know it solved the problem. How such tiny mistakes give us all big heads. I have it all the time.
Anyway, just started with JQuery aswel 10 days ago. It’s a nice and easy thing! Keep on going!
I did find the jquery-1.2.6.min.js was spelled differently jquery-1.2.6-min.js
note the ‘-’ before min in place of a ‘.’ before min
I changed that but still a blank screen,and no pop-up or error code.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<html>
<head>
<title> JQuery_Test </title>
<script type=“text/javascript” src=“jquery-1.2.6.min”></script>
<script type=“text/javascript” src=“script.js”></script>
</head>
<body>
<script type=“text/javascript”>
/<![CDATA[/
$(function() { alert(‘Ready to do your bidding!’); });
alert(“Hello World_xx”);
/]]>/
</script>
</body>
</html>
I guess there won’t be a problem in the jquery script:
<script type=“text/javascript” src=“jquery-1.2.6.min.js”></script>
But you forgot to put ".js " at the end right now.
But maybe there could be an error in the other script you’re loading:
<script type=“text/javascript” src=“script.js”></script>
I don’t know what that script does, maybe check aswel with a debugger.
<script type="text/javascript">
$(function() { alert('Ready to do your bidding!'); });
alert("Hello World_xx");
</script>
It’s good practice (in my opinion at least) to put some comments around javascript such that (very) old browsers don’t output it plaintext. Like so:
<script type="text/javascript">
[COLOR="SeaGreen"]/*<![CDATA[*/[/COLOR]
$(function() { alert('Ready to do your bidding!'); });
alert("Hello World_xx");
[COLOR="SeaGreen"]/*]]>*/[/COLOR]
</script>
Browsers that do not understand javascript won’t show it, because it’s enclosed in <![CDATA]>, whereas browsers that do understand javascript will ignore this <![CDATA]> because it’s in javascript comments
PS. For the purpose of being able to follow the book it’s probably a good idea to stick to jQuery 1.2.6 for now, but note that jQuery is currently on version 1.4.2