Home  >  Article  >  Backend Development  >  What should I do if the js output by PHP is not executed?

What should I do if the js output by PHP is not executed?

angryTom
angryTomOriginal
2019-11-04 17:01:173676browse

What should I do if the js output by PHP is not executed?

The js output by PHP is not executed

<?php
    echo "<script>alert(&#39;我弹出来了&#39;)</script>";
?>

You can output the JS script in PHP to the browser in one sentence implement. (Recommendation: "PHP Tutorial")

But today I encountered a very strange problem, that is, this code just outputs a string under Chrom and Firefox, and is not executed. An alert pops up.

Principle:

MIME chrome does not parse text/plain. PHP can use header to output html, and the browser uses the last output header as content-type

header(&#39;Content-Type:text/html;charset=utf-8&#39;);

text/plain html will not be executed.

Just need to re-header before alert.

if(!uploadFile())
{
    echo "<script>alert(&#39;上传文件失败&#39;)</script>";    
}
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
echo ("")

The above is the detailed content of What should I do if the js output by PHP is not executed?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn