C#實現頁面加載

protected void Page_Load(object sender, EventArgs e)
    {
        Loading();
    }


public void Loading()
    {
        HttpContext hc 
= HttpContext.Current;
        
//創建一個頁面居中的div
        hc.Response.Write("<div id='loading'style='position: absolute; height: 100px; text-align: center;z-index: 9999; left: 50%; top: 50%; margin-top: -50px; margin-left: -175px;'> ");
        hc.Response.Write(
"<br />頁面正在加載中,請稍候<br /><br /> ");
        hc.Response.Write(
"<table border='0' cellpadding='0' cellspacing='0' style='background-image: url(images/Progress/plan-bg.gif);text-align: center; width: 300px;'> ");
        hc.Response.Write(
"<tr><td style='height: 20px; text-align: center'><marquee direction='right' scrollamount='30' width='290px'> <img height='10' src='images/Progress/plan-wait.gif' width='270' />");
        hc.Response.Write(
"</marquee></td></tr></table></div>");
        
//hc.Response.Write("<script>mydiv.innerText = '';</script>");
        hc.Response.Write("<script type=text/javascript>");
        
//最重要是這句了,重寫文檔的onreadystatechange事件,判斷文檔是否加載完畢
        hc.Response.Write("function document.onreadystatechange()");
        hc.Response.Write(
@"{ try  
                                   {
                                    if (document.readyState == 'complete') 
                                    {
                                         delNode('loading');
                                        
                                    }
                                   }
                                 catch(e)
                                    {
                                        alert('頁面加載失敗');
                                    }
                                                        } 

                            function delNode(nodeId)
                            {   
                                try
                                {   
                                      var div =document.getElementById(nodeId); 
                                      if(div !==null)
                                      {
                                          div.parentNode.removeChild(div);   
                                          div=null;    
                                          CollectGarbage(); 
                                      } 
                                }
                                catch(e)
                                {   
                                   alert('刪除ID爲'+nodeId+'的節點出現異常');
                                }   
                            }

                            
");

        hc.Response.Write(
"</script>");
        hc.Response.Flush();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章