简体   繁体   English

当使用php标头功能时,为什么警报不起作用? .i知道window.location.href是解决方案

[英]why alert don't work when we use php header function ? .i know window.location.href is solution

why alert function don't work in the following snippet? 为什么警报功能在以下代码段中不起作用?

<?php
ob_start();
echo "<script>alert('Some text');</script>";
header("location:somepage.php");
?>

It is redirecting the user but not showing the alert . 它正在重定向用户,但不显示警报。 I know window.location.href is the solution but i want the reason why this alert is not working. 我知道window.location.href是解决方案,但我想知道此警报不起作用的原因。

thanks in advance 提前致谢

Because the browser will execute the redirect before waiting for DOM to load completely. 因为浏览器将在等待DOM完全加载之前执行重定向。 Your page will tell the browser to redirect and the body of the page contain javaScript that will issue an alert after the DOM completed successfully. 您的页面将告诉浏览器重定向,并且页面正文包含javaScript,它将在DOM成功完成后发出警报。

And as standard practice, when using Header redirect you must exit the PHP and don't output anything before setting any header attribute. 按照惯例,使用Header重定向时,必须退出PHP并且在设置任何header属性之前不输出任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM