首页 > 编程技术 > html

防IE内存泄漏办法

发布时间:2016-9-20 19:02

<div id="d1"></div>
<script >
function createButton(){
         var obj = document.createElement("button");
          obj.innerHTML="点我!";
          obj.onclick=function(){
                      //处理click事件
            }
          obj.onmouseover=function(){
                   //处理mouseover事件
         }
        return obj;//这里由于需要返回创建的对象,所以不能把obj直接设为null. return 后obj是局部变量,不能在外部断开其与HTMLElement的引用.ie中将出现问题泄漏问题
}
var 按钮 = document.getElementsById("d1").appendChild( createButton());
按钮.做某些事();
按钮.做某些事();
........
某些东西.某些事(按钮);
......
</script>


这种写法在IE中100%内存泄漏

使用try finally很容易解决些问题
function createButton(){
         var obj = document.createElement("button");
          obj.innerHTML="点我!";
          obj.onclick=function(){
                      //处理click事件
            }
          obj.onmouseover=function(){
                   //处理mouseover事件
         }
        try{
            return obj;
        }finally{
              obj = null;//这句话在return 之后才执行 , 的效的解决了需在return后将obj置null的问题
       }
}


一个函数或方法中,其实有很多地方都需要这种选返回值,最后执行某些事的

动态设置上传文件enctype类型以下是最常见上传文件片段 
<form id="upform" name="upform" action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile[]" />
<input type="file" name="uploadfile[]" />
<button type="submit" >upload</button>
</form>
有点js知识得人可能试图用js检查并控制enctype得类型
 


form=document.getElementById('upform');
if(form.getAttribute('enctype')!='multipart/form-data'){
    form.setAttribute('enctype','multipart/form-data');
}
以上代码完全按照W3C格式
FF下运行良好 但是IE不兼容,其它浏览器可以不兼容,就IE不能不兼容
在看YUI类库时得到启发
IE中form没有enctype属性,只有encoding属性,那么以上代码改写为
 


form=document.getElementById('upform');
if(form.getAttribute('enctype')!='multipart/form-data'&&form.encoding!='multipart/form-data')
  if(form.encoding){
      form.setAttribute('encoding','multipart/form-data');
  }else{
      form.setAttribute('enctype','multipart/form-data');
  }
}
//最外成if判断可以去掉 因为你是要设置它可以上传文件
这样你可以试试这样得代码了
 


<script type="text/javascript">
function upload(form){
  if(form.encoding){
      form.setAttribute('encoding','multipart/form-data');
  }else{
      form.setAttribute('enctype','multipart/form-data');
  }
  form.setAttribute('method',post');
  if(!form.getAttribue('action')){
      form.setAttribute('action',location.href);
  }
  form.submit();
}
</script>
<form action="upload.php" onsubmit="try{upload(this);}catch(e){};return false;">
<input type="file" name="uploadfile[]" />
<input type="file" name="uploadfile[]" />
<button type="submit" >upload</button>
</form>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="imagetoolbar" content="no">
<title>js数组实现</title>
<style>
<!--
body{text-align:center;}
table{
 border:0px;
}
.sp{
 width:520px;
 height:400px;
 border:2px solid #FFCC00;
 text-align:center;
 line-height:400px;
}
.sn{
 width:520px;
 height:30px;
 text-align:center;
 line-height:30px;
}
img{
 border:0px; 
 cursor:pointer;
}
-->
</style>
<script language="javascript">
<!--
var photo=new Array(4);
var photoname=new Array(4);


photo[0]="photo/1.jpg"
photoname[0]="柳梦璃"

photo[1]="photo/2.jpg"
photoname[1]="韩菱纱"

photo[2]="photo/3.jpg"
photoname[2]="云天河"

photo[3]="photo/4.jpg"
photoname[3]="慕容紫英"


currid=0

function showphoto(){
currid=currid+1;
if(currid>=4){
currid=0;
}
inphoto.src=photo[currid];
fn.innerHTML=photoname[currid];
}
-->
</script>

</head>
<body>

<table>
 <tr>
  <td class="sp"><img name="inphoto"" width=100% src="photo/1.jpg" onclick="showphoto()" alt="点击图片显示下一张"></td>
 </tr>
 <tr>
  <td class="sn"><span id="fn">柳梦璃</span></td>
 </tr>
 <tr>
  <td class="sn">JS数组实现点击图片显示下一张</td>
 </tr>
</table>

</body>

</html>


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="imagetoolbar" content="no">
<title>翻页原理实现</title>
<style>
<!--
body{text-align:center;}
table{
 border:0px;
}
.sp{
 width:520px;
 height:400px;
 border:2px solid #FFCC00;
 text-align:center;
 line-height:400px;
}
.sn{
 width:520px;
 height:30px;
 text-align:center;
 line-height:30px;
}
img{
 border:0px; 
}
-->
</style>

</head>
<body>

<table>
 <tr>
  <td class="sp"><a href="2.asp?page=4"><img name="inphoto"" width=100% src="photo/3.jpg" alt="点击图片显示下一张"></a></td>
 </tr>
 <tr>
  <td class="sn"><span id="fn">云天河</span></td>
 </tr>
 <tr>
  <td class="sn">利用翻页原理实现点击图片显示下一张</td>
 </tr>
</table>

</body>

</html>


大家可以发现很多网站都把验证码改成在点击输入框时才显示的。包括,qzone,baidu,discuz等。

我来说下这样做有以下两点好处。

1.减轻服务器负载
比如说回复需要输入验证码,而实际上我们知道会回复的比率很底,假如帖子打开1000次只有10个人回复了,那么990次加载验证码负载都是浪费掉的

2.验证码实时正确性

比如我们同时打开两个帖子,验证码使用的是同一个cookie/session变量,当打开第一个页面时假如验证码为1111,第二次条开时为2222,实际上第二次的验证码已经把第一次的覆盖了,这时两个验证码实际上都是2222,这种情况就会造成输入正确的验证码实际上也是错的。

验证码改成在点击输入框时才显示后,验证码就是实时的了。
标签:[!--infotagslink--]

您可能感兴趣的文章: