61
// if the box is outside the window, move it to the end
function checkEdge() {
    var windowsLeftEdge = $('#window').position().left;

    $('.box').each( function(i, box) {
        // right edge of the sliding box
        var boxRightEdge = $(box).position().left + $(box).width();

        // position of last box + width + 10px
        var newPosition = getNewPosition();

        if ( parseFloat(boxRightEdge) < parseFloat(windowsLeftEdge) ) { 
            $(box).css('left', newPosition);
            $(box).remove().appendTo('#window');
            first = $('.box:first').attr('class');
        }
    });
}​ //Uncaught SyntaxError: Unexpected token ILLEGAL Occurs Here

// arrange the boxes to be aligned in a row
function arrangeBoxes() {
    $('.box').each( function(i, item) {
        var position = $('#window').position().left + i * ( $(item).width());
        $(item).css('left', position+'px')
    });
}

// shifts all the boxes to the left, then checks if any left the window
function shiftLeft() {
    $('.box').animate({'left' : "-=100px"}, 5000, 'linear', checkEdge());
}

// returns the new location for the box that exited the window
function getNewPosition() {
    return $('.box:last').position().left + $('.box:last').outerWidth();
}

$(window).load(function() {
      arrangeBoxes();
    shiftLeft();
    setInterval('shiftLeft()', 5000);

    $('#gallery-slideshow').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        slices:15,
        animSpeed:500, //Slide transition speed
        pauseTime:3000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:true, //Next & Prev
        directionNavHide:true, //Only show on hover
        controlNav:false, //1,2,3...
        keyboardNav:false, //Use left & right arrows
        pauseOnHover:false, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, //Triggers after all slides have been shown
        lastSlide: function(){}, //Triggers when last slide is shown
        afterLoad: function(){} //Triggers when slider has loaded
    });

});

$(document).ready(function(){

    $('.class-table tr').click(function(){
        window.location=$(this).find("a").attr("href"); return false;
    });

    $('.special-workshop').click(function(){
        window.location=$(this).find("a").attr("href"); return false;
    });

});

I am getting an Uncaught SyntaxError: Unexpected token ILLEGAL on the line mentioned above. It occurs only in Google Chrome and Safari. It works in Firefox and the same code works on this JSBin (http://jsbin.com/uceqi/18)

What is going on?

There are numerous references to this problem on Stackoverflow but none of them seem to apply to this situation.

If it helps JSLint also throws and error on that line character 2 "Problem at line 22 character 2: Unexpected '​'."

Patrick Arlt
  • 1,225
  • 2
  • 11
  • 16
  • Possible duplicate of [No visible cause for "Unexpected token ILLEGAL"](http://stackoverflow.com/questions/12719859/no-visible-cause-for-unexpected-token-illegal) – Michał Perłakowski Dec 29 '15 at 15:18

11 Answers11

136

Delete all invisible characters (whitespace) around that area, then give it another try.

I've seen that error in Safari when copy/pasting code. You can pick up some invalid (and unfortunately invisible) characters.

Used to happen to me a lot when copying from jsFiddle.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • 36
    same, don't copy paster from jsFiddle :) – Mihai Oprea Mar 03 '12 at 18:12
  • 3
    Yep, do not copy paste from jsFiddle. Pasted it into Text Edit, saved as plain text and then re-copy pasted it and it worked. – Foxinni Aug 27 '12 at 15:23
  • For those that missed it: copying and pasting code from jsFiddle could potentially lead to an issue with whitespace that is, as yet, unfixed in october 2012. – Vael Victus Oct 15 '12 at 15:21
  • 1
    I have found this occurs when there are carriage return (`\r`) characters in the code. Annoyingly these won't display easily in the source. – James McCormack Oct 25 '12 at 12:47
  • 2
    well, the commenters above pointed it, but I feel the urge to point it out myself, DO NOT COPY PASTE FROM jsFiddle ! – SAFAD Dec 04 '12 at 11:36
  • 9
    Hello, this may come as a shock to you - but you rumour has it that it is not a wise decision to copy/paste from jsFiddle. – hallizh Dec 12 '12 at 09:30
  • I got it from copying from stackoverflow, I thought it was some kind of strange variation of curly braces. – Niels Brinch Apr 11 '13 at 19:53
  • Also look out for ' and " that are copied/pasted. – KrisF May 07 '13 at 02:43
  • If you're tired; you might want to check you aren't trying to load a CSS file via ` – William Isted Feb 21 '14 at 09:50
  • or don't be lazy and write the code yourself without copy+paste :p – StinkyCat Mar 05 '14 at 12:49
  • Additionally you could use text editors like Notepad++ and to find unwanted characters. – QMaster Jun 07 '14 at 09:04
14

It doesn't apply to this particular code example, but as Google food, since I got the same error message:

<script>document.write('<script src="…"></script>');</script>

will give this error but

<script>document.write('<script src="…"><'+'/script>');</script>

will not.

Further explanation here: Why split the <script> tag when writing it with document.write()?

Community
  • 1
  • 1
Henrik N
  • 15,786
  • 5
  • 82
  • 131
  • 1
    Fixed it for me. Thanks! Also, in the link you provided it actually says to separate the string between `<` and `/`, like `'<' + '/script>'`. – Gavin Jun 28 '14 at 20:14
  • 1
    @Gavin Glad it helped! And good point; updated my answer. (It originally had `' – Henrik N Jun 28 '14 at 21:27
6

I got the same error when the script file I was including container some special characters and when I was running in local moode (directly from local disk). I my case the solution was to explicitly tell the encoding:

<script src="my.js" charset="UTF-8"></script>
Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43
6

Note for anyone running Vagrant: this can be caused by a bug with their shared folders. Specify NFS for your shared folders in your Vagrantfile to avoid this happening.

Simply adding type: "nfs" to the end will do the trick, like so:

config.vm.synced_folder ".", "/vagrant", type: "nfs"
loglesby
  • 109
  • 1
  • 3
  • what about for window users who cannot use nfs? – matthew Oct 28 '14 at 01:21
  • You legend! Had to `rm -rf node_modules` and run `npm cache clean` before running `npm install` again but it finally worked. Thanks for minimising the **hours** I wasted on this. – madebydavid Aug 11 '16 at 18:02
4

Another possible cause for Googlers: Using additional units in a size like so:

$('#file_upload').uploadify({
    'uploader'  : '/uploadify/uploadify.swf',
    'script'    : '/uploadify/uploadify.php',
    'cancelImg' : '/uploadify/cancel.png',
    'folder'    : '/uploads',
    'queueID'        : 'custom-queue',
    'buttonImg': 'img/select-images.png',
    'width': '351px'
});

Setting '351px' there gave me the error. Removing 'px' banished the error.

Mere Development
  • 2,439
  • 5
  • 32
  • 63
2

When in doubt... use JSLint to get it out!

http://www.jslint.com

I just ran into a similar problem whilst copying this from JFiddle;

$('input[name=MeetAll]').change(function (e) {
  $('#MeetMost').attr('checked', !$('#MeetAll').attr('checked'));
});
$('input[name=MeetMost]').change(function (e) {
  $('#MeetAll').attr('checked', !$('#MeetMost').attr('checked'));
});​

Jslint told me i had a random "." Charachter...

Things that make you go "hmmmmmm"

Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51
2

Also for the Google-fodder: check in your text editor whether the .js file is saved as Unicode and consider setting it to ANSI; also check if the linefeeds are set to DOS and consider switching them to Unix (depending on your server, of course).

Tom Auger
  • 19,421
  • 22
  • 81
  • 104
  • Also check that you don't have a Byte Order Mark as the first few characters in a Hex editor – rwired Aug 26 '12 at 07:30
1

It won't be exactly refering to the given problem, but I wanna share my mistake here, maybe some1 will make simmilar one and will also land with his/her problem here:

Ive got Unexpected token ILLEGAL error because I named a function with a number as 1st char.

It was 3x3check(). Changing it to check3x3() solved my problem.

pbialy
  • 1,025
  • 14
  • 26
1

Double backslash also works ! Then you declare there really should be a / instead of some function or something.

<script>document.write('<script src="…"><//script>');</script>
Jelmer
  • 19
  • 1
0

This error can also be caused by a javascript line like this one:

navi_elements.style.bottom = 20px;

Notice that the value is not a string.

reggie
  • 3,523
  • 14
  • 62
  • 97
0

You can use online Minify, it removes these invisible characters efficiently but also changes your code. So be careful.

http://jscompress.com/

Xdg
  • 1,735
  • 2
  • 27
  • 42