使用Chrome:Uncaught SyntaxError:意外的令牌<
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.
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.
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.
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:
</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.
我收到此错误未捕获的SyntaxError:来自chrome的意外标记&lt; strong>但一切正常 使用FireFox很好。
我发现了许多类似的帖子,但没有解决方案。 p>
所以我想知道是否有一种方法可以在浏览器构建之后向浏览器发送第二页,并使用它自己的标题。
当我看到Firefox放置时,我的想法就来了 ,我在下面的函数中回显,在html结束标记 em>之后,而Chrome将它放在结束标记之前。 p>
基本上,我喜欢发送这个 顺序: p>
这是我的php脚本,我想取消注释标题代码并从html页面独立发送。 p>
这就是Firefox中页面的样子,这有效: p>
我以为我可以使用ob_start()&amp; ob_end_flush(),但您无法控制仅包含该内容的标题。 p>
div>
header('Content-Type:text / html; charset = utf-8');
&lt; html&gt;&lt; / html&gt;
header( 'Content-Type:text / javascript; charset = utf-8');
&lt; script&gt;&lt; / script&gt;
code> pre>
public static function jsShow($ html)
{
// header('Content-Type:text / javascript; charset = utf-8');
echo“
&lt; script type = \”text / javascript \“&gt;
var e = document.getElementById('message');
e.innerHTML = $ html;
e.style.display =' 阻止';
&lt; / script&gt;“;
}
code> pre>
&lt; / body&gt;
&lt; / html&gt;
&lt; script type =“text / javascript”&gt;
var e = document.getElementById('message'); \ ne.innerHTML =&lt; ul style =“list-style:none; margin:0; padding:0;”&gt;
&lt; li style =“background-color:#0000FF; margin:0;”&gt;&lt; img src =“/ asset / icon / info.gif”alt =“信息:”/&gt; 工作&lt; / li&gt;
&lt; li style =“background-color:#008000; margin:0;”&gt;&lt; img src =“/ asset / icon / success.gif”alt =“成功:”/&gt; 得到它&lt; / li&gt;
&lt; / ul&gt;
;
e.style.display ='block';
&lt; / script&gt;
code> pre>
...
e.innerHTML = " . json_encode($html) . " ;
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:
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:
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:
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.