0

我有一个 Ajax 回调函数,它调用 PHP 函数检查文件修改时间。我使用计时器每 50 秒执行一次。

    <script type="text/javascript">
    setInterval("_checkPopUpUpdate()", 50000); //50 seconds
    </script>

    function _checkPopUpUpdate()
    {
        var callback=new Object();
        callback.success=this.onExternalSuccess;
        callback.failure=this.onExternalFailure;
        YAHOO.util.Connect.asyncRequest('GET','/ci/ajaxCustom/ajaxCheckPopupUpdate',callback);
};

PHP函数检查文件修改时间与第一次加载会话时间是否不同

     $announcement_Popup_Path = HTMLROOT . $this->data['attrs']['file_path'];
  // Call model function

 if($this->data['attrs']['file_path'] !== '' && file_exists($announcement_Popup_Path))
 {
     //When the announcement content load, it always stores the modified time into session
         $this->CI->load->model('custom/checkupdate_model');
           $firstloadTime = filemtime($announcement_Popup_Path);
     $this->CI->checkupdate_model->store_AnnouncementPopupSession($firstloadTime);
     //$this->data['Popup'] = file_get_contents($announcement_Popup_Path);
 }    

        function store_AnnouncementPopupSession ($popupTime)
    {
 $sessionPopupTime =array("annoucementPopUp"=> $popupTime);
 $this->session->setSessionData($sessionPopupTime);
    }

     function announcement_pop()
    {
 $file_path='/euf/assets/announcements/pop_up_announcement.html';
 $announcement_Popup_Path = HTMLROOT . $file_path;
 if(file_exists($announcement_Popup_Path)) {
     $currentAnnouncementPopUpTime = filemtime($announcement_Popup_Path);
     $oldannouncementPopupTime = $this->session->getSessionData("annoucementPopUp");

     if($currentAnnouncementPopUpTime !=$oldannouncementPopupTime) {
  //comparing the content and update the time, when they are different,  send back update content
  $this->store_AnnouncementPopupSession($currentAnnouncementPopUpTime);
  //echo $currentAnnouncementPopUpTime;
  echo $file_path;
     }
     else {
  echo "no update";
     }
 }
    }

当文件修改时间发生变化时,它会返回ajax回调并抓取我的web服务器中的HTML内容来弹出一个通知弹出窗口。

    function onExternalSuccess (o){
    if(o.responseText!==undefined)
    {
 var str=o.responseText;


     if(str !== 'no update') // Then pop up.
     {
  L=screen.width-200;
  T=screen.height;
  popup=window.open(str," ",'alwaysRaised=yes,status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=no,scrollbars=no,height=150,width=364,left="+L+",top="+T');
  //popup=window.open(str,"",'alwaysRaised=yes,status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=no,scrollbars=no,height=100,width=330');
  for (i=0;i<200;i++)
  {
      T=T-1;
      popup.moveTo(L,T);
  }
     }
    }
};

它适用于 Firefox 和 Chrome,但 IE7 有点错误,有时弹出窗口不出来。

<html>
    <head>
 <title>Internal alert</title>
 <link rel="stylesheet" type="text/css" href="/euf/assets/css/pop_up_ann.css" media="screen, projection" />
 <script type="text/javascript">
    var howLong = 50000; //5 minutes, 1 seconds = 1000 milliseconds.
    t = null;
    function closeMe(){
    t = setTimeout("self.close()",howLong);
    }
      </script>
    </head>
    <body onLoad="closeMe();self.focus();">
 <div id="cs_popup">
        <div class="popup_rounded-corner-top">
            <div id="popup_ann">
                <div id="popup_ann_title">IE7 pop-up EMEA test close </div>
                <div id="popup_ann_from"> PLC team - 05/01/2011 at 12:10</div>
            <div id="popup_ann_content">
            Something has changed in the<em>&quot;Alerts&quot;</em> section of S4S since your last visit.
            Make sure you go there as soon as you can to be informed about the current situation.
            </div>
            </div>
        </div>
        <div class="popup_rounded-corner-bottom"></div>
    </div>
    </body>
</html>

我在这里遇到的主要问题是自关闭功能。它也关闭了我的弹出窗口和网站。有什么提示吗?另外,我不确定我的整个弹出公告逻辑结构是否正确,有吗?谢谢

4

1 回答 1

1

为这样的公告打开弹出窗口对于运行弹出窗口阻止程序的人来说可能是有问题的,这通常只允许弹出窗口出现以响应用户发起的事件,例如点击某物。更好的方法是使用内联弹出窗口,如果您要求他们确认您的消息,它还可以让您有机会以模态方式显示弹出窗口(即用半透明的 div 屏蔽页面的其余部分)。

self.close() 的使用不应该关闭用户打开的原始窗口/选项卡,它应该只关闭已经“打开”的东西,所以我不确定那里发生了什么,我怀疑你没有告诉我们一切 :) 另一种方法可能是修改您的弹出功能以关闭窗口,而不是让窗口自行关闭。

// open popup ... then set timeout to close it
var popupDelay = 50000;
setTimeout(function() {
  popup.close();
}, popupDelay);

这可能会更好一些,不确定。但从长远来看,我强烈建议避免弹出窗口并使用内联的东西。

于 2011-01-05T13:28:07.453 回答