php输出的js不执行
<?php echo "<script>alert('我弹出来了')</script>";?>
一句话就可以在php里面输出js脚本让浏览器执行。(推荐:《php教程》)
但是今天碰到一个很诡异的问题,就是这段代码在chrom和firefox下只是单纯的输出字符串,没有执行,没有弹出alert。
原理:
mime chrome对text/plain 不做解析的。php利用header可以输出html ,浏览器用最后一次输出header作为content-type
header('content-type:text/html;charset=utf-8');
text/plain html是不执行的。
只需要在alert前重新header即可。
if(!uploadfile()){ echo "<script>alert('上传文件失败')</script>"; }header('content-type:text/html;charset=utf-8');echo ("<script type='text/javascript'>alert('保存成功');history.back();</script>")
以上就是php输出的js不执行怎么办的详细内容。