开发者

Pop up window self close function; close the site as well

I have an Ajax call back function which call to a PHP function check the file modified time. I uses timer to execute it every 50 seconds.

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

    function _checkPopUpUpdate()
开发者_如何学Python    {
        var callback=new Object();
        callback.success=this.onExternalSuccess;
        callback.failure=this.onExternalFailure;
        YAHOO.util.Connect.asyncRequest('GET','/ci/ajaxCustom/ajaxCheckPopupUpdate',callback);
};

The PHP function checks whether the file modified time has different between the first load session time

     $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";
     }
 }
    }

When the file modifed time has changed, it will return to ajax call back and grab the HTML content in my web server to Pop up an annoucement pop up window.

    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);
  }
     }
    }
};

It works fine for Firefox and Chrome, however IE7 is a bit bugy, sometime the pop up does not come out.

<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>

The main problem i have here it is self-close function. It close my pop up window and the website as well. Any hints? Also, i am not sure whether my whole pop up annoucement logical structure is correct, is there anyway? thanks


Opening a popup window for an announcement like this could be problematic for people running popup blockers, which typically only allow popups to appear in response to a user-initiated event like clicking on something. A better approach would be to use an inline popup which would also give you the opportunity to display the popup modally (i.e. mask the rest of the page with a semi-transparent div) if you require them to acknowledge your message.

The use of self.close() should not close the original window/tab opened by the user, it should only close things that have been 'open'ed so I'm not sure what's going on there, I suspect you haven't told us everything :) An alternate approach might be to modify your popup function to close the window rather than have the window close itself.

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

This may work a little better, not sure. But in the long run, I highly recommend avoiding popups and using something inline instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜