0

If I copy and paste this into my web page:

<script>
    $(function(){
        $("#startDate").datepicker();
    });​​​
</script>

I get this error in the browser:

Uncaught SyntaxError: Unexpected token ILLEGAL

However, using that exact same code, if I simply delete the final semi-colon and add it again, the code gets formatted within VS 2012 and then it works. Why? I didn't think spacing mattered with JavaScript?

And if I paste that same code and add the spacing myself, it still doesn't work. I have to type it all by hand, or delete and add the semi-colon again. Could this be a bug? Am I missing something elementary?

This is the entire code within the page:

@{
    Layout = null;
}

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <script src="~/Scripts/jquery-1.7.1.min.js"></script>
        <script src="~/Scripts/jquery-ui-1.8.20.min.js"></script>
        <script>
            $(function(){
                $("#startDate").datepicker();
            });​​​
        </script>
    </head>
    <body>
        <div>
            <h2>Home View</h2>
            <input type="text" id="startDate">
        </div>
    </body>
    </html>

And just to be complete, this is the code that works:

<script>
    $(function () {
        $("#startDate").datepicker();
    });
</script>

I don't see the difference, other than the spacing. And like I said, I can have that exact code, with the spacing, and it fails unless I delete and add the semi-colon.

Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • 1
    Take the "broken" code and copy it into Notepad++ then go into View -> Show Symbol and select Show All Characters. Do you see anything that shouldn't be there? – Shane Courtrille Sep 03 '12 at 19:17
  • possible duplicate of [Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL](http://stackoverflow.com/questions/5733275/chrome-uncaught-syntax-error-unexpected-token-illegal) – Pointy Sep 03 '12 at 19:19
  • Did you possibly try that code in jsfiddle and then copy/paste from there? That seems to be a common cause of this. – Pointy Sep 03 '12 at 19:27
  • Yes, I believe I did, actually. But things were broken before I even tried fiddle. (Hence trying fiddle at all.) – Bob Horn Sep 03 '12 at 19:28
  • I would appreciate the downvoter explaining himself so I can learn from it. I'm not sure how this can be considered a duplicate when I had no idea that was the problem. – Bob Horn Sep 03 '12 at 19:29

2 Answers2

1

Look at the code in your browser - hit view source - and double check that your IDE is not adding some extra codes that are breaking the javascript.

Ctrl-U in chrome to view source.

Dave Hilditch
  • 5,299
  • 4
  • 27
  • 35
  • This should not get a downvote as it is precisely correct. There's probably a weird unicode space character in there. *edit* well maybe "precisely" is a little strong, as we don't know what the character is. – Pointy Sep 03 '12 at 19:20
  • The browser source didn't show the garbage characters, but Notepadd++ did. Thanks for the tip. – Bob Horn Sep 03 '12 at 19:25
1

There are some junk characters after the semi-colon that need to be cleaned up.

If I paste the exact text you have in SO into Wordpad++, then it shows this:

<script>
    $(function(){
        $("#startDate").datepicker();
    });???
</script>

Which shows some illegal characters.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I see that now, too. However, when I paste it into my code file, within VS, those characters aren't there. If I put the cursor immediately behind the semi-colon, I have to hit delete four times before the semi-colon is gone. So, yeah, there is some junk there. Not sure how/why. – Bob Horn Sep 03 '12 at 19:22
  • So it wasn't really deleting the semi-colon that did it. It was the fact that I was deleting the garbage (on my way to deleting the semi-colon) that did it. Thank you. – Bob Horn Sep 03 '12 at 19:24
  • @BobHorn yes that's right. I answered this same question a long time ago, and because the question gets upvotes all the time I suspect it's a common problem caused by the same badly-behaved editor (VisualStudio itself possibly). – Pointy Sep 03 '12 at 19:25
  • @BobHorn - What your editor displays where there is some non-ascii stuff in there is completely dependent upon the editor. The point is that there is junk in there that needs to be cleaned up. – jfriend00 Sep 03 '12 at 19:26