javascript 使用 Chrome: Uncaught SyntaxError: Unexpected token <
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8697211/
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
With Chrome: Uncaught SyntaxError: Unexpected token <
提问by RoboTamer
I am getting this error Uncaught SyntaxError: Unexpected token <from chrome but everything works fine with FireFox. I have found many similar posts but no solution.
我收到此错误Uncaught SyntaxError: Unexpected token <from chrome 但在 FireFox 中一切正常。我找到了很多类似的帖子,但没有解决方案。
So I am wondering if there is a way of sending a second page to the browser after it has been build, with it's own header. The idea came, when I saw that Firefox places, what I echo in the function below, after the html closing tag , versus Chrome places it before the closing tag.
所以我想知道是否有一种方法可以在构建后将第二页发送到浏览器,并带有自己的标题。这个想法来了,当我看到 Firefox 放置时,我在下面的函数中回显,在 html 结束标记之后, 与 Chrome 相比,将它放在结束标记之前。
Basically, I like to send in this order:
基本上,我喜欢按这个顺序发送:
header('Content-Type: text/html; charset= utf-8');
<html></html>
header('Content-Type: text/javascript; charset= utf-8');
<script></script>
This is my php script, I would like to uncomment the header code and send it independently from the html page.
这是我的 php 脚本,我想取消注释标题代码并独立于 html 页面发送它。
public static function jsShow($html)
{
//header('Content-Type: text/javascript; charset= utf-8');
echo "
<script type=\"text/javascript\">
var e = document.getElementById('message');
e.innerHTML = $html ;
e.style.display = 'block';
</script>";
}
This is what the page looks like in Firefox, and this works:
这是页面在 Firefox 中的样子,并且有效:
</body>
</html>
<script type="text/javascript">
var e = document.getElementById('message');
e.innerHTML = <ul style="list-style: none; margin: 0; padding: 0;">
<li style="background-color: #0000FF; margin: 0;"><img src="/asset/icon/info.gif" alt="Info: " /> working</li>
<li style="background-color: #008000; margin: 0;"><img src="/asset/icon/success.gif" alt="Success: " /> Got it</li>
</ul>
;
e.style.display = 'block';
</script>
I thought that I can maybe use ob_start() & ob_end_flush(), but you can't control headers with that only content.
我认为我可以使用 ob_start() 和 ob_end_flush(),但是您无法仅使用该内容来控制标题。
回答by Ignacio Vazquez-Abrams
...
...
e.innerHTML = " . json_encode($html) . " ;
回答by nnnnnn
The error "Uncaught SyntaxError: Unexpected token <" is an appropriate error for what you are showing in your second code block, because it is invalid JavaScript and should fail in all browsers. You are assigning e.innerHTML
equal to an unquoted string:
错误“Uncaught SyntaxError: Unexpected token <”是您在第二个代码块中显示的适当错误,因为它是无效的 JavaScript,应该在所有浏览器中失败。您正在分配e.innerHTML
等于未加引号的字符串:
e.innerHTML = <ul style="list-style: none; margin: 0; padding: 0;">
<li style="background-color: #0000FF; margin: 0;"><img src="/asset/icon/info.gif" alt="Info: " /> working</li>
<li style="background-color: #008000; margin: 0;"><img src="/asset/icon/success.gif" alt="Success: " /> Got it</li>
</ul>
;
See the "<" right after the "="? - that is the unexpected token.
看到“=”后面的“<”了吗?——那是意料之外的令牌。
You don't seem to use any single quotes within that string, so the simplest fix is just to wrap it in single quotes. I don't know PHP, but something like this:
您似乎没有在该字符串中使用任何单引号,因此最简单的解决方法就是将其用单引号括起来。我不知道 PHP,但像这样:
echo "
<script type=\"text/javascript\">
var e = document.getElementById('message');
e.innerHTML = '" . $html . "';
e.style.display = 'block';
</script>";
Or whatever the PHP syntax is to get this result returned to the browser:
或者无论 PHP 语法是什么让这个结果返回到浏览器:
e.innerHTML = '<ul style="list-style ... </ul>';
By the way, it doesn't make sense to set two different headers for the same response. What you are returning is one page that is html, which happens to have a script block at the bottom. The "text/javascript" content type is more for linked JS files, which do not contain html (no script tags), just JS.
顺便说一句,为同一个响应设置两个不同的标头是没有意义的。您返回的是一个 html 页面,它恰好在底部有一个脚本块。“text/javascript”内容类型更多用于链接的 JS 文件,它不包含 html(没有脚本标签),只包含 JS。