Javascript Chrome 未捕获的语法错误:意外的令牌非法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5733275/
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 18:37:36  来源:igfitidea点击:

Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL

javascriptjquerygoogle-chrome

提问by agmcleod

Receiving the subject error when Chrome tries to load the script file on the page. It says it's at the last line of the javascript file. I can't seem to find anything wrong with it. No errors in firefox, and the script works as expected. Just using form validation

当 Chrome 尝试在页面上加载脚本文件时收到主题错误。它说它在 javascript 文件的最后一行。我似乎找不到任何问题。firefox 中没有错误,脚本按预期工作。仅使用表单验证

// JavaScript Document
$(function() {
  $('#wm-form').submit(function() {
    var errors = false;
    var errorMsg = "";
    $('.required').each(function() {
      if(!validField($(this))) {
        errorMsg += $(this).attr('name').capitalize() + " cannot be blank\n";
        errors = true;
      }
    });
    var emailAddress = $('#email');
    if(isValid(emailAddress) && !(/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/.test(emailAddress.val()))) {
      errorMsg += "Not a valid email address. Please enter in a correctly formatted email address";
      errors = true;
    }
    if(errors) {
      alert(errorMsg);
      return false;
    }
  });

  $('.form-focus').click(function() {
    $(document).scrollTop(0);
    $('#first_name').focus();
    return false;
  });
});

function validField(element) {
  if(!isValid(element.val()) || (element.attr('placeholder') && element.attr('placeholder') == element.val()) || 
    (element.attr('type') == 'radio' && !checkedRadio(element))) {
    return false;
  }
  else {
    return true;
  }
}

function isValid(ele) {
  if(ele == null || ele == '') {
    return false;
  }
  else {
    return true;
  }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

function checkedRadio (element) {
  var valid = false;
  $('input[name="'+ element.attr("name") +'"]:checked').each(function() {
    valid = true;
  });

  return valid;
}?

回答by Pointy

There's some sort of bogus character at the end of that source. Try deleting the last line and adding it back.

在该来源的末尾有某种虚假的字符。尝试删除最后一行并将其添加回来。

I can't figure out exactly what's there, yet ...

我无法弄清楚那里到底有什么,但......

edit— I think it's a zero-width space, Unicode 200B. Seems pretty weird and I can't be sure of course that it's not a Stackoverflow artifact, but when I copy/paste that last function including the complete last line into the Chrome console, I get your error.

编辑- 我认为这是一个零宽度空间,Unicode 200B。看起来很奇怪,我当然不能确定它不是 Stackoverflow 工件,但是当我将最后一个函数(包括完整的最后一行)复制/粘贴到 Chrome 控制台时,我收到了您的错误。

A notorious source of such characters are websites like jsfiddle. I'm not saying that there's anything wrong with them — it's just a side-effect of something, maybe the use of content-editable input widgets.

此类字符的臭名昭著的来源是jsfiddle之类的网站。我并不是说它们有什么问题——这只是某些东西的副作用,也许是使用了内容可编辑的输入小部件。

If you suspect you've got a case of this ailment, and you're on MacOS or Linux/Unix, the odcommand line tool can show you (albeit in a fairly ugly way) the numeric values in the characters of the source code file. Some IDEs and editors can show "funny" characters as well. Note that such characters aren't alwaysa problem. It's perfectly OK (in most reasonable programming languages, anyway) for there to be embedded Unicode characters in string constants, for example. The problems start happening when the language parser encounters the characters when it doesn't expect them.

如果您怀疑自己患有此病,并且您使用的是 MacOS 或 Linux/Unix,则od命令行工具可以向您显示(尽管以相当丑陋的方式)源代码文件字符中的数值. 一些 IDE 和编辑器也可以显示“有趣”的字符。请注意,此类字符并不总是一个问题。例如,在字符串常量中嵌入 Unicode 字符是完全可以的(无论如何,在大多数合理的编程语言中)。当语言解析器遇到不希望出现的字符时,问题就开始发生了。

回答by KurtWM

I get the same error in Chrome after pasting code copied from jsfiddle.

粘贴从 jsfiddle 复制的代码后,我在 Chrome 中遇到了同样的错误。

If you select all the code from a panel in jsfiddle and paste it into the free text editor Notepad++, you should be able to see the problem character as a question mark "?" at the very end of your code. Delete this question mark, then copy and paste the code from Notepad++ and the problem will be gone.

如果您从 jsfiddle 的面板中选择所有代码并将其粘贴到自由文本编辑器 Notepad++ 中,您应该能够将问题字符视为问号“?” 在代码的最后。删除这个问号,然后从 Notepad++ 复制粘贴代码,问题就解决了。

回答by Eye

I had the same error when multiline string included new line (\n) characters. Merging all lines into one (thus removing all new line characters) and sending it to a browser used to solve. But was very inconvenient to code.

当多行字符串包含新行 ( \n) 字符时,我遇到了同样的错误。将所有行合并为一个(从而删除所有新行字符)并将其发送到用于解决的浏览器。但是编码非常不方便。

Often could not understand why this was an issue in Chrome until I came across to a statement which said that the current version of JavaScript engine in Chrome doesn't support multiline strings which are wrapped in single quotes and have new line (\n) characters in them. To make it work, multiline string need to be wrapped in double quotes. Changing my code to this, resolved this issue.

通常无法理解为什么这是 Chrome 中的一个问题,直到我遇到一个声明,该声明说 Chrome 中当前版本的 JavaScript 引擎不支持用单引号括起来并\n在其中包含换行 ( ) 字符的多行字符串. 为了使它工作,多行字符串需要用双引号括起来。将我的代码更改为此,解决了这个问题。

I will try to find a reference to a standard or Chrome doc which proves this. Until then, try this solution and see if works for you as well.

我将尝试找到可以证明这一点的标准或 Chrome 文档的参考。在此之前,请尝试此解决方案,看看是否也适合您。

回答by Dragos Durlut

I had the same error in Chrome. The Chrome console told me that the error was in the 1st line of the HTML file.

我在 Chrome 中遇到了同样的错误。Chrome 控制台告诉我错误在 HTML 文件的第一行。

It was actually in the .js file. So watch out for setValidNou(1060, $(this).val(), 0')error types.

它实际上在 .js 文件中。所以要注意setValidNou(1060, $(this).val(), 0')错误类型。