0

Often ( too often ) I get this error in chrome console. I really can't find any syntacs error in my javascript code

When I try to locate where this error happen in google chrome, it always shows this:

chrome javascript error

It shows that error is on the beginning of the file ( somewhere around DOCTYPE ). Is this just that chrome doesn't show more infos or I messed up with inclusion of the files somewhere? ( or maybe even syntacs? ). Either way, I can't locate it with help of debug console


this is global.js:

(function() { 

    $("#btadd").click(function() { 

        $(".newlist").slideUp();
        return false;

    });


})();

this is html:

<div class="newlist" style="display:none">
<h4>Title: <input type="text"/></h4>
</div>
<br />
   <a href="javascript:void()" id="btadd"><span>Add New</span></a>
3
  • 1
    You'll need to post some code or an example. Commented Feb 16, 2013 at 17:12
  • 3
    The SyntaxError won't be your DOCTYPE - go through your JavaScript Commented Feb 16, 2013 at 17:13
  • code added , this is basically all I have Commented Feb 16, 2013 at 17:18

2 Answers 2

5

I had a similar problem. The error could also come from

javascript:void();

which should actually be

javascript:void(0);

You can reproduce this error in your console with

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

Comments

2

Change this:

(function() { 
    $("#btadd").click(function() { 
        $(".newlist").slideUp();
        return false;
    });
})();

to this:

$(function () {
    $("#btadd").click(function () {
        $(".newlist").slideUp();
        return false;
    });
});

Or move your existing JavaScript to the end of the body. You created a self-invoking anonymous function that's executing before the elements it applies to have been loaded. This would work fine once the DOM is fully loaded, but since it's in the head of your document it runs too early. By using the example I showed you you're using jQuery's document ready event to run the code.

6 Comments

This can't be right. If that was the case, then the error would be something like a TypeError or something being null or undefined, as the DOM elements could not be found. But the error thrown is apparently a SyntaxError so there must be something else wrong. As there doesn't appear to be any syntax errors in the javascript posted I guess there is more script included somewhere on the page, on one of those scripts contains a syntax error.
@AHM - no, it's correct. You can reproduce this easily in a jsFiddle.
Why the downvote? It's obviously correct and the OP verified it.
For the two anonymous cowards that downvoted, care to explain why?
I would downvote, because even though though the OP "verified" it, it is not clear s/he did so knowingly. Your answer is solving an entirely different problem. The other answer, which was commented on by the OP, is clearly the correct one.
|

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.