1

Does anyone have any idea why the code below is not working?

 <!DOCTYPE html>
<head>
    <title> The wonderfulness of JS and jQuery </title>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-latest.min.js">
    <script type="text/javascript">
        function writeIt() {
           document.write("jQuery version " + $().jquery + " loaded.");
        }
    </script>
</head>
<body onload="writeIt()">
Hello
</body>
</html> 

It is not displaying the jQuery version. I just came across that piece of code when trying to learn some jQuery and JavaScript and wondering why it is not working for me.

Thanks!

2 Answers 2

4

It's just because you didn't end the script tag...

<script src="http://code.jquery.com/jquery-latest.min.js"> </script>

For the script tag it's absolutely required. It needs to be fully qualified, it doesn't end with />.

3
  • Thanks. The jQuery version is showing up, but anything that I put in the <body> is not.. How can I get them both to work?
    – Niralana
    Commented Dec 7, 2014 at 14:54
  • 2
    Don't use document.write, this overwrites any content. Use innerHTML or $.append instead!
    – metadings
    Commented Dec 7, 2014 at 14:55
  • Sorry, I'm new to StackOverflow and not sure how I can accept the answers... As soon as I figure it out, I'll do this!
    – Niralana
    Commented Dec 8, 2014 at 15:44
0

This answer is based on your comment part,

<!DOCTYPE html>
<head>
<title> The wonderfulness of JS and jQuery </title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
    function writeIt() {
       alert("jQuery version " + $().jquery + " loaded.");
       /*document.getElementById("maincontent").innerHTML='Body Content';*/
        $('#maincontent').append('appended content');
       /*document.write("jQuery version " + $().jquery + " loaded.");*/

       /*if (typeof jQuery != 'undefined') {  
            alert(jQuery.fn.jquery);
         }
        }*/
    }

</script>
</head>
<body id="maincontent" onload="writeIt()">
 initial content 
</body>
</html> 
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.