简体   繁体   English

使用php和javascript在FF和Chrome中无法正常显示警报错误

[英]Display an alert error with php and javascript not working properly in FF and Chrome

I use this piece of code to inform user to fill some field in this way: 我使用这段代码通知用户以这种方式填写一些字段:

ob_start();  
$msg = $_SESSION['ErrorUser'];

if($msg !=""){
    echo '<script type="text/javascript">alert(" ' . $msg . '");</script>';
}

//echo $msg;
$_SESSION['ErrorUser']  = "";                 
ob_end_flush();

This works in Internet explorer while in FF and Chrome. 这适用于在FF和Chrome中的Internet Explorer。 The msg is displayed, but I have nothing in background, just a part of my page is showed (behind my Message alert). 显示消息,但我在后台没有任何内容,只显示了我的页面的一部分(在我的消息警报后面)。

The problem you're facing is that your outputting the alert straight into your dom which will cause the alert to be triggered while the dom is still loading. 您面临的问题是您将警报直接输入dom,这将导致在dom仍在加载时触发警报。 If you're using jQuery you should output the alert box inside the jQuery(document).ready callback or otherwise bind to the load / DOMcontentready events of the window object. 如果你正在使用jQuery,你应该在jQuery(document).ready回调中输出alert框,或以其他方式绑定到window对象的load / DOMcontentready事件。

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

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