Whenever I use the u2028 character literal in my javascript source with the content type set to "text/html; charset=utf-8" I get a javascript parse errors.
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>json</title>
<script type="text/javascript" charset="utf-8">
var string = '? ';
</script>
</head>
<body>
</body>
</html>
If the <meta http-equiv>
is left out everything works as expected. I've tested this on Safari and Firefox, both exhibit the same problem.
Any ideas on why this is happening and how to properly fix this (without removing the encoding)?
Edit:
After some more research, the specific problem was that the problem character was returned using JSONP. This was then interpreted by the browser, which reads u2028 as a newline and throws an error about an invalid newline in a string.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…