【问题标题】:Unexpected token ILLEGAL in webkitwebkit中的意外令牌非法
【发布时间】:2011-05-23 05:11:30
【问题描述】:
// 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;
    });

});

我在上面提到的行上收到 Uncaught SyntaxError: Unexpected token ILLEGAL。它仅出现在 Google Chrome 和 Safari 中。它适用于 Firefox,同样的代码适用于这个 JSBin (http://jsbin.com/uceqi/18)

发生了什么事?

在 Stackoverflow 上有很多关于这个问题的参考,但似乎没有一个适用于这种情况。

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

【问题讨论】:

标签: javascript jquery google-chrome safari


【解决方案1】:

它不会完全指代给定的问题,但我想在这里分享我的错误,也许 some1 会犯类似的错误并且也会在这里遇到他/她的问题:

我收到Unexpected token ILLEGAL 错误,因为我将一个数字命名为第一个字符的函数。

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

【讨论】:

    【解决方案2】:

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

    http://jscompress.com/

    【讨论】:

      【解决方案3】:

      这个错误也可能是由这样的 javascript 行引起的:

      navi_elements.style.bottom = 20px;
      

      请注意,该值不是字符串。

      【讨论】:

        【解决方案4】:

        它不适用于这个特定的代码示例,但作为 Google 食物,因为我收到了相同的错误消息:

        <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()?

        【讨论】:

        • 为我修复了它。谢谢!此外,在您提供的链接中,它实际上说要分隔&lt; 和/ 之间的字符串,例如'&lt;' + '/script&gt;'。
        • @Gavin 很高兴它有帮助!好点;更新了我的答案。 (它最初有'&lt;script src="…"&gt;&lt;/'+'script&gt;'。)
        【解决方案5】:

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

        只需将type: "nfs" 添加到末尾即可,如下所示:

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

        【讨论】:

        • 不能使用nfs的window用户怎么办?
        • 你的传奇!在再次运行npm install 之前,必须rm -rf node_modules 并运行npm cache clean,但它终于奏效了。感谢您最大限度地减少我在这方面浪费的 小时。
        【解决方案6】:

        当脚本文件包含容器一些特殊字符以及在本地穆德(直接从本地磁盘)中运行时,我得到了同样的错误。我的情况是,解决方案是明确告诉编码:

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

        【讨论】:

          【解决方案7】:

          如有疑问...使用 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 告诉我我有一个随机的“。”字符...

          让你“hmmmmmm”的事情

          【讨论】:

            【解决方案8】:

            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”消除了错误。

            【讨论】:

              【解决方案9】:

              同样适用于 Google-fodder:在您的文本编辑器中检查 .js 文件是否保存为 Unicode,并考虑将其设置为 ANSI;还要检查换行符是否设置为 DOS 并考虑将它们切换到 Unix(当然取决于您的服务器)。

              【讨论】:

              • 还要检查您在十六进制编辑器中没有将字节顺序标记作为前几个字符
              【解决方案10】:

              双反斜杠也有效!然后你声明确实应该有一个 / 而不是某个函数或其他东西。

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

              【讨论】:

                【解决方案11】:

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

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

                从 jsFiddle 复制时经常发生在我身上。

                【讨论】:

                • 一样,不要从 jsFiddle 复制粘贴 :)
                • 是的,不要从 jsFiddle 复制粘贴。将其粘贴到文本编辑中,另存为纯文本,然后重新复制粘贴即可。
                • 我发现当代码中有回车符 (\r) 时会发生这种情况。恼人的是,这些不会轻易显示在源代码中。
                • 好吧,上面的评论者指出了这一点,但我觉得自己指出的冲动,不要从 jsFiddle 复制粘贴!
                • 您好,这可能会让您感到震惊 - 但您有传言说从 jsFiddle 复制/粘贴不是明智的决定。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-11-07
                • 1970-01-01
                • 2015-10-19
                • 1970-01-01
                相关资源
                最近更新 更多