Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL

agmcleod picture agmcleod · Apr 20, 2011 · Viewed 446k times · Source

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

Answer

Pointy picture Pointy · Apr 20, 2011

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.