【问题标题】:onbeforeunload trigger popup only when onload not finishedonbeforeunload 仅在 onload 未完成时触发弹出窗口
【发布时间】:2012-05-19 15:05:04
【问题描述】:

我正在尝试确定用户是否在页面加载完成之前离开,但我在页面加载完成后遇到了一些问题,因为我不知道如何禁用它。 这是搜索加载页面的一部分,因此当页面加载完成时,用户会被重定向到结果页面,但使用此脚本会询问用户是否要在搜索完成后离开页面。 代码如下,适用于页面加载未完成和完成时。 关于如何仅在页面加载完成之前触发此事件的任何想法,或者有没有其他方法可以实现这一点?

谢谢。

<?php

$var = <<<EOF

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
    <script language="JavaScript">
      window.onbeforeunload = confirmExit;
      function confirmExit()
      {
        return "message?";
      }
    </script>
</head>
<body>
asdf
</body>
</html>

EOF;

    print $var;
    flush();
    ob_flush();    
$i = 0;
while($i < 10){
    print $i.'<br />';
    flush();
    ob_flush();    
    sleep(1);
    $i++;
}

?>

【问题讨论】:

    标签: php javascript onload onbeforeunload


    【解决方案1】:
    window.onload = function(){
      // Called once the page is fully loaded to make onbeforeunload a no-op 
      window.onbeforeunload = null;
    }
    

    确保这是文档中的第一个函数,因此它首先被绑定。

    【讨论】:

      猜你喜欢
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      相关资源
      最近更新 更多