弹出窗口自闭功能;关闭该网站

时间:2011-01-05 12:45:54

标签: ajax call back

我有一个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>

我在这里遇到的主要问题是自动关闭功能。它关闭了我的弹出窗口和网站。任何提示?另外,我不确定我的整个弹出式通知逻辑结构是否正确,无论如何?谢谢

1 个答案:

答案 0 :(得分:1)

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

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

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

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