2010-12-10 91 views
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; 
    }); 

}); 

我得到一个未捕获的SyntaxError:上面提到的行上的意外的令牌非法。它仅在Google Chrome和Safari中出现。它在Firefox中工作,并且相同的代码适用于此JSBin(http://jsbin.com/uceqi/18webkit中的意外令牌非法

发生了什么事?

在Stackoverflow上有很多这个问题的引用,但他们似乎都不适用于这种情况。

如果有帮助JSLint也会抛出该错误并在该行上出现字符2“第22行字符问题:意外'”。

+0

[ “非法意外令牌” 无明显原因(http://stackoverflow.com/questions/12719859/no-visible-cause-for-unexpected-token-illegal) – 2015-12-29 15:18:08

回答

136

删除该区域周围的所有不可见字符(空白),然后再次尝试。

我在复制/粘贴代码时看到了Safari中的错误。你可以选择一些无效的(但不可见的)字符。

从jsFiddle复制时,曾经发生过很多事情。

+36

的可能的复制同样,不要从jsFiddle复制贴纸:) – 2012-03-03 18:12:13

+3

是的,不要从jsFiddle复制粘贴。将其粘贴到文本编辑中,保存为纯文本,然后重新复制粘贴它,它工作。 – Foxinni 2012-08-27 15:23:11

+1

不要从jsFiddle复制和粘贴代码! – Joe 2012-09-28 10:22:24

14

它并不适用于这个特定的代码示例,但作为谷歌的食物,因为我得到了同样的错误信息:

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

会给这个错误,但

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

不会。这里

进一步解释:Why split the <script> tag when writing it with document.write()?

1

双反斜杠也可以!然后你声明真的应该有一个/而不是某个函数或某个东西。

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

还为谷歌与饲料:检查在文本编辑器的.js文件是否被保存为Unicode,并考虑将其设置为ANSI;还要检查是否将换行符设置为DOS并考虑将其切换到Unix(当然取决于您的服务器)。

4

,让Google另一个可能的原因:在尺寸使用附加的单位,像这样:

$('#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' 
}); 

设置“351px”有给我的错误。删除'px'消除了错误。

2

如果有疑问......请使用JSLint将它搞定!

http://www.jslint.com

我只是碰到了,而从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告诉我,我有一个随机的“。” Charachter ...

东西,让你去“hmmmmmm”

6

我得到了同样的错误时,脚本文件,我包括集装箱一些特殊的字符,当我在本地运行moode(直接从本地磁盘) 。我在我的情况下,解决办法是明确地告诉编码:

<script src="my.js" charset="UTF-8"></script> 
1

它不会完全指的给定的问题,但我想在这里分享我的失误,也许some1将使simmilar之一,也将与土地他/她的问题在这里:

我得到了Unexpected token ILLEGAL错误,因为我用一个数字命名了一个函数作为第一个字符。

这是3x3check()。 更改为check3x3()解决了我的问题。

6

注意运行Vagrant的任何人:这可能是由于共享文件夹存在错误。在Vagrantfile中为共享文件夹指定NFS以避免发生这种情况。

简单地增加type: "nfs"到底会做的伎俩,像这样:

config.vm.synced_folder ".", "/vagrant", type: "nfs" 
0

此错误也可以通过一个javascript线像这样的原因造成的:

navi_elements.style.bottom = 20px; 

注意,值不是一个字符串。

0

您可以使用在线Minify,它可以有效地删除这些不可见的字符,但也会更改您的代码。所以要小心。

http://jscompress.com/