Chrome未捕获的语法错误:非法的令牌非法 [英] Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL

查看:223
本文介绍了Chrome未捕获的语法错误:非法的令牌非法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

SyntaxError:意外令牌非法




$ b 当Chrome尝试收到主题错误在页面上加载脚本文件。它说它在JavaScript文件的最后一行。我似乎无法找到任何问题。在Firefox中没有错误,并且脚本按预期工作。只需使用表单验证

  // JavaScript文档
$(function(){
$('# wm-form')。submit(function(){
var errors = false;
var errorMsg =;
$('。required')。each(function(){
if(!validField($(this))){
errorMsg + = $(this).attr('name')。capitalize()+不能空白\\\
;
错误=真;
}
});
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 + =不是有效的电子邮件地址,请输入正确格式的电子邮件地址;
errors = true;
}
if(errors){
alert(errorMsg);
返回false;
}
});

$('。单击(函数(){
$(document).scrollTop(0);
$('#first_name')。focus();
返回false;
});
});

函数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;



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



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

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

返回有效;


解决方案

字符在该源的末尾。尝试删除最后一行并将其添加回去。



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

编辑—我认为这是一个零宽度的空间,Unicode 200B。看起来很奇怪,我不能肯定它不是一个Stackoverflow神器,但是当我将包括完整最后一行在内的最后一个功能复制/粘贴到Chrome控制台时,我收到了错误信息。



这些人物的臭名昭着的来源是像 jsfiddle 这样的网站。我并不是说他们有什么不妥之处,它只是一些东西的副作用,也许是使用了内容可编辑的输入小部件。



如果你怀疑你有这种病的情况,在MacOS或Linux / Unix上, od 命令行工具可以向您显示源代码文件字符中的数字值(虽然方式相当丑陋)。一些IDE和编辑也可以显示有趣的字符。请注意,这些字符不是始终是一个问题。例如,在大多数合理的编程语言中,完全可以使用字符串常量来嵌入Unicode字符。当语言解析器遇到不想要的字符时,问题就开始发生。


Possible Duplicate:
SyntaxError: Unexpected token ILLEGAL

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

// 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;
}​

解决方案

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.

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.

If you suspect you've got a case of this ailment, and you're on MacOS or Linux/Unix, the od command 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 always a 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.

这篇关于Chrome未捕获的语法错误:非法的令牌非法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆