7

well I want to debug some script with Firebug, (cause I can't see anything in the browser window) but when I click the script tab in Firefox it gives me the error message:

If tags have a "type" attribute, it should equal "text/javascript" or "application/javascript". Also scripts must be parsable (syntactically correct).

What am I doing wrong?

Here's my code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>



<script src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
/* fix: “close” the value of i inside createFunction, so it won't change */
        var createFunction = function(i) {
            return function() { alert(i); };
        };

        for (var i=0; i<5; i++) {
            $('p').appendTo('body').on("click", createFunction(i));
}

})();
</script>
</body>
</html>
5
  • 1
    IT'S IN black-BOLD! in my text! Commented Jun 15, 2012 at 9:46
  • When I run that in my Firefox I get a Firebug error $(function () {var createFunction = function (i) {return function () {alert(i);};};for (var i = 0; i < 5; i++) {$("p").appendTo("body").on("click", createFunction(i));}}) is not a function Commented Jun 15, 2012 at 9:47
  • add type="text/javascript" to your first <script> tag Commented Jun 15, 2012 at 9:49
  • <script src="jquery-1.7.1.js" type="text/javascript"></script> Commented Jun 15, 2012 at 9:49
  • @KrisIvanov & nbrooks that's not necessary with html5. Commented Jun 15, 2012 at 9:55

3 Answers 3

4

You must leave out the last parenthesis, I guess the code should run on dom ready?

<script type="text/javascript">
$(function() {
/* fix: “close” the value of i inside createFunction, so it won't change */
        var createFunction = function(i) {
            return function() { alert(i); };
        };

        for (var i=0; i<5; i++) {
            $('<p>').appendTo('body').on("click", createFunction(i));
}

});
</script>

See here for how to make code running on dom load with jquery.

Sign up to request clarification or add additional context in comments.

3 Comments

@acne hey it works! thanks for dat! funny this is an syntax-error that firebug should point out...one more thing why Ican't see any in the browser? P should be append to the body uhn?
You must use <p> when creating elements, otherwise it's just a plain selector for jquery. jQuery searches for an existing p element and tries to append that to the body. Because there does not exist one, nothing gets appended. I updated my answer.
Also worth noting that since you have an HTML5 doctype, type="text/javascript" is optional.
1

Remove parenthesis after }):

$(function() {
/* fix: “close” the value of i inside createFunction, so it won't change */
     var createFunction = function(i) {
        return function() { alert(i); };
     };

     for (var i=0; i<5; i++) {
            $('p').appendTo('body').on("click", createFunction(i));
     }    
});  //here is the modification

Comments

1

In my Case I have opened firebug in other tab, So it was showing me this error.

  • Solution : I have closed one tab and refreshed the page. and it was working :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.