iframe

关键字: ajax/javascript相关
    以前的项目中用到了Ajax,采用的是Ajaxanywhere和iframe实现的局部刷新,现在做的项目中也要用到Ajax,顺便想起了iframe的刷新,虽然iframe的刷新不是异步的,但效果也相当不错.在网上也看到许多帖子里也写到了,在这里写上两个例子,也算是总结了,但有的代码是网上的例子改的,因为有的有点问题我都改了过来,可以直接运行看到效果的.
例一iframe.html代码如下:
Html代码 复制代码
  1. <html>  
  2. <head>  
  3.  <title>Example of remote scripting in an IFRAME</title>  
  4. <script >  
  5.  function tosay(){   
  6.     alert('this function is called from server.html');   
  7.  }   
  8.  function target(){   
  9.     var ifrm = document.getElementsByName('passframe')[0];   
  10.     ifrm = document.getElementById('passframe');   
  11.     ifrm.src = 'server.html';   
  12.  }   
  13. </script>  
  14. </head>  
  15. <body>  
  16. <h1>Remote Scripting with an TFRAME </h1>  
  17. <iframe id="passframe" name="passframe" style="width:0px;height:0px;border:0px;" src="about:blank"></iframe>  
  18. <a href="javascript:target();">call the server</a>  
  19. <input type="button" value="button" src="server.html" onclick="target();">  
  20. </body>  
  21. </html>  

server.html代码如下:
Html代码 复制代码
  1. <html>  
  2. <head>  
  3. <title>the server</title>  
  4. </head>  
  5. <script>  
  6. //window.parent.tosay();   
  7. function init(){   
  8.     alert("server page init");   
  9.     parent.tosay();   
  10. }   
  11. </script>  
  12. <body onload="init();">  
  13. </body>  
  14. </html>  


例二,a.html代码:
Html代码 复制代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.     <head>  
  4.         <title>a.html</title>  
  5.   
  6.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  7.         <meta http-equiv="description" content="this is my page">  
  8.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  9.   
  10.         <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  
  11.         <script>    
  12.                
  13.             function changeDiv(){    
  14.                 var iframeObj = document.getElementById("workFrame");    
  15.                 iframeObj.src="b.html";    
  16.             }    
  17.             function changeFrom(){    
  18.                 document.formName.target="workFrame";    
  19.                 document.formName.submit();    
  20.             }    
  21. </script>  
  22.   
  23.     </head>  
  24.   
  25.     <body>  
  26.         <input type="button" name="" value="Change Div" onclick="changeDiv();">  
  27.         <input type="button" name="" value="Form div" onclick="changeFrom();">  
  28.         <a onclick="changeDiv();">changeDiv</a>  
  29.         <DIV id="divId">  
  30.             Main page Content   
  31.         </DIV>  
  32.         <Form id="formId" name="formName" action="b.html">  
  33.   
  34.         </Form>  
  35.         <iframe id="workFrame" width="0" height="0" src=""></iframe>  
  36.     </body>  
  37. </html>  


b.html代码:
Html代码 复制代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.     <head>  
  4.         <title>b.html</title>  
  5.   
  6.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  7.         <meta http-equiv="description" content="this is my page">  
  8.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  9.   
  10.         <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  
  11.         <script>    
  12.         function init(){   
  13.             try{    
  14.                 alert("page b");   
  15.                 var div = document.getElementById("divId");    
  16.                 var parentparent_div = parent.document.getElementById("divId");    
  17.                 if(div){    
  18.                     parent_div.innerHTML = div.innerHTML;    
  19.                 }      
  20.             }catch(e){   
  21.                 alert(e);   
  22.             }   
  23.         }   
  24. </script>    
  25.            
  26.     </head>  
  27.   
  28.     <body onload="init();">  
  29.         <DIV id="divId">  
  30.             iframe page result content .   
  31.         </DIV>  
  32.   
  33.     </body>  
  34. </html>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值