Javascript webkit 中的意外令牌非法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4404526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:17:18  来源:igfitidea点击:

Unexpected token ILLEGAL in webkit

javascriptjquerygoogle-chromesafari

提问by Patrick Arlt

// 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)

我在上面提到的行上收到 Uncaught SyntaxError: Unexpected token ILLEGAL。它仅出现在 Google Chrome 和 Safari 中。它适用于 Firefox,同样的代码适用于这个 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.

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

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

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

回答by user113716

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.

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

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

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

回答by Henrik N

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

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

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

这里进一步解释:为什么在使用 document.write() 编写 <script> 标签时将其拆分?

回答by Juha Palom?ki

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>

回答by loglesby

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.

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

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

简单地添加type: "nfs"到最后就可以了,如下所示:

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

回答by Mere Development

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.

在那里设置“351px”给了我错误。删除 'px' 消除了错误。

回答by Naruto Sempai

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

如有疑问……使用 JSLint 来解决它!

http://www.jslint.com

http://www.jslint.com

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

我在从 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...

Jslint 告诉我我有一个随机的“。” 性格...

Things that make you go "hmmmmmm"

让你“嗯嗯”的事情

回答by Tom Auger

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).

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

回答by Jelmer

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>

回答by pbialy

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 ILLEGALerror because I named a function with a number as 1st char.

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

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

它是3x3check()。更改它以check3x3()解决我的问题。

回答by reggie

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

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

navi_elements.style.bottom = 20px;

Notice that the value is not a string.

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