iframe 中加载 PHP 并重定向的 Javascript 不会执行


Javascript in iframe that loads a PHP and redirects is not executed

>我有这个PHP代码:

<?php
/*
 DOING SOME PHP STUFF THAT WORKS HERE
*/
echo "<script type='"text/javascript'">
alert('test1');
</script>";
header("Location: index.php");
?>

这个 php 页面在 iframe 中加载。重定向工作正常,但 javascript 未执行。我的猜测是服务器不发送此页面的 HTML,而只是执行 PHP 代码,然后发送重定向 HTML。

我可以让javascript先执行,然后再重定向吗?

JS上的一行代码就可以使用window.location

<?php
echo "<script>alert('test1');window.location.href='index.php'</script>";

只需通过javascript进行重定向,请尝试以下操作:

echo "<script type='"text/javascript'">
alert('test1');
document.location.href='index.php';
</script>";