J2EE社區(qū)

          茍有恒,何必三更起五更眠;
          最無(wú)益,只怕一日曝十日寒.
          posts - 241, comments - 318, trackbacks - 0, articles - 16

          js刷新頁(yè)面 方法大全

          Posted on 2010-05-29 11:30 xcp 閱讀(53730) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): Javascript
          本文轉(zhuǎn)載于:http://www.jb51.net/article/14397.htm

          一、先來(lái)看一個(gè)簡(jiǎn)單的例子:
                  下面以三個(gè)頁(yè)面分別命名為frame.htmltop.htmlbottom.html為例來(lái)具體說(shuō)明如何做。 
                  
                  frame.html 由上(top.html)下(bottom.html)兩個(gè)頁(yè)面組成,代碼如下:
           1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
           2<HTML> 
           3<HEAD> 
           4<TITLE> frame </TITLE> 
           5</HEAD> 
           6<frameset rows="50%,50%"> 
           7   <frame name=top src="top.html"> 
           8   <frame name=bottom src="bottom.html"> 
           9</frameset> 
          10</HTML> 
               
                  現(xiàn)在假設(shè)top.html (即上面的頁(yè)面) 有七個(gè)button來(lái)實(shí)現(xiàn)對(duì)bottom.html (即下面的頁(yè)面) 的刷新,可以用以下七種語(yǔ)句,哪個(gè)好用自己看著辦了。      
           1top.html 頁(yè)面的代碼如下: 
           2
           3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
           4<HTML> 
           5<HEAD> 
           6<TITLE> top.html </TITLE> 
           7</HEAD> 
           8<BODY> 
           9  <input type=button value="刷新1" onclick="window.parent.frames[1].location.reload()"><br> 
          10  <input type=button value="刷新2" onclick="window.parent.frames.bottom.location.reload()"><br> 
          11  <input type=button value="刷新3" onclick="window.parent.frames['bottom'].location.reload()"><br>  
          12  <input type=button value="刷新4" onclick="window.parent.frames.item(1).location.reload()"><br> 
          13  <input type=button value="刷新5" onclick="window.parent.frames.item('bottom').location.reload()"><br> 
          14  <input type=button value="刷新6" onclick="window.parent.bottom.location.reload()"><br> 
          15  <input type=button value="刷新7" onclick="window.parent['bottom'].location.reload()"><br> 
          16</BODY> 
          17</HTML> 
          18
                    
                     下面是bottom.html頁(yè)面源代碼,為了證明下方頁(yè)面的確被刷新了,在裝載完頁(yè)面彈出一個(gè)對(duì)話(huà)框。  
           1
           2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
           3<HTML> 
           4<HEAD> 
           5<TITLE> bottom.html </TITLE> 
           6</HEAD> 
           7<BODY onload="alert('我被加載了!')"> 
           8   <h1>This is the content in bottom.html.</h1> 
           9</BODY> 
          10</HTML> 

                        解釋一下:
          1.window指代的是當(dāng)前頁(yè)面,例如對(duì)于此例它指的是top.html頁(yè)面。 
          2.parent指的是當(dāng)前頁(yè)面的父頁(yè)面,也就是包含它的框架頁(yè)面。例如對(duì)于此例它指的是framedemo.html。 
          3.frames是window對(duì)象,是一個(gè)數(shù)組。代表著該框架內(nèi)所有子頁(yè)面。 
          4.item是方法。返回?cái)?shù)組里面的元素。 
          5.如果子頁(yè)面也是個(gè)框架頁(yè)面,里面還是其它的子頁(yè)面,那么上面的有些方法可能不行。 

          附: 

          Javascript刷新頁(yè)面的幾種方法: 
          1 history.go(0) 
          2 location.reload() 
          3 location=location 
          4 location.assign(location) 
          5 document.execCommand('Refresh') 
          6 window.navigate(location) 
          7 location.replace(location) 
          8 document.URL=location.href 

          二、自動(dòng)刷新頁(yè)面
                  1.頁(yè)面自動(dòng)刷新:把如下代碼加入<head>區(qū)域中       
          <meta http-equiv="refresh" content="20"> 
          其中20指每隔20秒刷新一次頁(yè)面. 
                 2.頁(yè)面自動(dòng)跳轉(zhuǎn):把如下代碼加入<head>區(qū)域中
          <meta http-equiv="refresh" content="20;url=http://xcp.blogjava.net"> 
          其中20指隔20秒后跳轉(zhuǎn)到http://xcp.blogjava.net頁(yè)面 
                3.頁(yè)面自動(dòng)刷新js版
          <script language="JavaScript"> 
              
          function myrefresh() 
              

                     window.location.reload(); 
              }
           
              setTimeout('myrefresh()',
          1000); //指定1秒刷新一次 
          </script> 


          三、java在寫(xiě)Servler,Action等程序時(shí),要操作返回頁(yè)面的話(huà)(如談出了窗口,操作完成以后,關(guān)閉當(dāng)前頁(yè)面,刷新父頁(yè)面)
          1        PrintWriter out = response.getWriter();
          2        out.write("<script  type=\"text/javascript\">");
          3         ////子窗口刷新父窗口 
          4         out.write("self.opener.location.reload();"); 
          5         //關(guān)閉窗口
          6         out.write("window.opener=null;");
          7         out.write("window.close();");
          8         out.write("</script>");

          四、JS刷新框架的腳本語(yǔ)句
                  1.如何刷新包含該框架的頁(yè)面用
          <script language=JavaScript> 
            parent.location.reload(); 
          </script> 
                
                 2.子窗口刷新父窗口
          <script language=JavaScript> 
              self.opener.location.reload(); 
          </script> 
               
                 3.如何刷新另一個(gè)框架的頁(yè)面用 (上面的實(shí)例以說(shuō)明了)
          語(yǔ)句1. window.parent.frames[1].location.reload(); 
          語(yǔ)句2. window.parent.frames.bottom.location.reload(); 
          語(yǔ)句3. window.parent.frames[
          "bottom"].location.reload(); 
          語(yǔ)句4. window.parent.frames.item(
          1).location.reload(); 
          語(yǔ)句5. window.parent.frames.item('bottom').location.reload(); 
          語(yǔ)句6. window.parent.bottom.location.reload(); 
          語(yǔ)句7. window.parent['bottom'].location.reload(); 
              
                 4.如果想關(guān)閉窗口時(shí)刷新或者想開(kāi)窗時(shí)刷新的話(huà),在<body>中調(diào)用以下語(yǔ)句即可。 
          <body onload="opener.location.reload()"> 開(kāi)窗時(shí)刷新 
          <body onUnload="opener.location.reload()"> 關(guān)閉時(shí)刷新 
          <script language="javascript"> 
              window.opener.document.location.reload() 
          </script> 



          名稱(chēng): ?4C.ESL | .↗Evon
          口號(hào): 遇到新問(wèn)題?先要尋找一個(gè)方案乄而不是創(chuàng)造一個(gè)方案こ
          mail: 聯(lián)系我


          Feedback

          # re: js刷新頁(yè)面 方法大全  回復(fù)  更多評(píng)論   

          2011-12-17 23:10 by 博而優(yōu)則賺
          很有用,收藏了

          # re: js刷新頁(yè)面 方法大全  回復(fù)  更多評(píng)論   

          2012-09-23 15:31 by ss
          dfdafd
          主站蜘蛛池模板: 新巴尔虎左旗| 宜章县| 隆德县| 巨野县| 无极县| 双城市| 新和县| 塔城市| 新安县| 工布江达县| 高邑县| 宁都县| 隆安县| 阜南县| 宁津县| 定边县| 务川| 上虞市| 海丰县| 泸州市| 中西区| 伽师县| 岫岩| 庆阳市| 金堂县| 六盘水市| 平江县| 卢氏县| 南通市| 上栗县| 宝坻区| 城口县| 平顶山市| 丁青县| 临湘市| 广汉市| 台东县| 凤城市| 平顶山市| 四川省| 高安市|