JavaScript垃圾回收机制

一、垃圾回收的必要性

下面这段话引自《JavaScript权威指南(第四版)》

  由于字符串、对象和数组没有固定大小,所有当他们的大小已知时,才能对他们进行动态的存储分配。JavaScript程序每次创建字符串、数组或对象时,解释器都必须分配内存来存储那个实体。只要像这样动态地分配了内存,最终都要释放这些内存以便他们能够被再用,否则,JavaScript的解释器将会消耗完系统中所有可用的内存,造成系统崩溃。

这段话解释了为什么需要系统需要垃圾回收,JS不像C/C++,他有自己的一套垃圾回收机制(Garbage Collection)。JavaScript的解释器可以检测到何时程序不再使用一个对象了,当他确定了一个对象是无用的时候,他就知道不再需要这个对象,可以把它所占用的内存释放掉了。例如: Continue reading

js里面如何获取网络地址的图片的高度和宽度 (变通办法)

原图片是 :<img src="http://gg.blueidea.com/2008/blueidea/flash2008.gif">

<div id="box"></div>

<script language="javascript">
(function(){
var img=document.createElement('img');//创建一个img元素
img.src="http://gg.blueidea.com/2008/blueidea/flash2008.gif";//指定src
img.style.position="absolute";//防止正常的内容变形
img.style.visibility='hidden';//藏起来
var inj=document.getElementById('box').appendChild(img);//插入到box中。当然插入到document.body也可以
alert('宽:'+inj.offsetWidth);//然后就可以通过 offset 取得宽和高了
alert('高:'+inj.offsetHeight);
})();
</script>

简单地说就是把图片放入一个自动伸缩的DIV中,然后获取DIV的宽和高!

js实现手动层效果

<html>
<body>
<style id="css">
body{font-family:Verdana;font-size:11px;color:#333;}
#win{position:absolute;left:100px;top:100px;width:200px;height:150px;border:1px solid #000;}
.title{width:100%;background:#000;height:18px;color:#fff;cursor:hand;}
</style>
<script>
var x0=0,y0=0,x1=0,y1=0;
var moveable=false;
//开始拖动;
function startDrag(obj){
if(event.button==1){
obj.setCapture();
var win = obj.parentNode;
x0 = event.clientX;
y0 = event.clientY;
x1 = parseInt(win.offsetLeft);
y1 = parseInt(win.offsetTop);
moveable = true;
}
}
//拖动;
function drag(obj){
if(moveable){
var win = obj.parentNode;
win.style.left = x1 + event.clientX - x0;
win.style.top = y1 + event.clientY - y0;
}
}
//停止拖动;
function stopDrag(obj){
if(moveable){
obj.releaseCapture();
moveable = false;
}
}
</script>
<div id="win">
<div onmousedown="startDrag(this)" onmouseup="stopDrag(this)" onmousemove="drag(this)">窗口1</div>
<div>
现在可以动了.<br>
删了很多代码,自己看看吧.<br>
</div>
</div>
</body>
</html>

JS内存释放问题

一个内存释放的实例

<SCRIPT LANGUAGE="JavaScript">
<!--
strTest = "1";
for ( var i = 0; i < 25; i ++ )
{
strTest += strTest;
}
alert(strTest);
delete strTest;
CollectGarbage();
//-->
</SCRIPT>

CollectGarbage, 是IE的一个特有属性,用于释放内存的使用方法嘛应该是,将该变量或引用对象,设置为null或delete然后在进行释放动作
在 做CollectGarbage前,要必需清楚的两个必备条件:

引用
- 一个对象在其生存的上下文环境之外,即会失效。
- 一个全局的对象在没有被执用(引用)的情况下,即会失效。 Continue reading

js过滤word格式

function cleanAndPaste(html) ...{
// Remove all SPAN tags
html = html.replace(/</?SPAN[^>]*>/gi, "" );
// Remove Class attributes
html = html.replace(/<(w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove Style attributes
html = html.replace(/<(w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
// Remove Lang attributes
html = html.replace(/<(w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove XML elements and declarations
html = html.replace(/<\??xml[^>]*>/gi, "") ;
// Remove Tags with XML namespace declarations: <o:p></o:p>
html = html.replace(/</?w+:[^>]*>/gi, "") ;
// Replace the &nbsp;
html = html.replace(/&nbsp;/, " " );
// Transform <P> to <DIV>
var re = new RegExp("(<P)([^>]*>.*?)(</P>)","gi") ; // Different because of a IE 5.0 error
html = html.replace( re, "<div$2</div>" ) ;

return html;
}

php中常用的全选函数

<script>function checkall(form, prefix, checkall) {
for(var i = 0; i < form.elements.length; i++) {
var e = form.elements[i];
if(e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
e.checked = form.elements[checkall].checked;
}
}
}</script>
<form>
<input type="checkbox" name="chkall" onclick="checkall(this.form, 'del', 'chkall')">全选
<input type="checkbox" name="del[]" value=1>删除1
<input type="checkbox" name="del[]" value=2>删除2
<input type="checkbox" name="del[]" value=3>删除3
<input type="checkbox" name="del[]" value=4>删除4
<input type="submit" name="modsubmit" value="提交">
</form>
checkall函数中的"del"为表单中del[]这类的前缀,"chkall"为单击的表单

ie下不支持javascript的解决办法

rem =====解决IE不支持javascript之批处理法开始========

regsvr32 actxprxy.dll /s
regsvr32 shdocvw.dll /s
Regsvr32 URLMON.DLL /s
Regsvr32 actxprxy.dll /s
Regsvr32 shdocvw.dll /s
regsvr32 oleaut32.dll /s
rundll32.exe advpack.dll /DelNodeRunDLL32 C:\WINNT\System32\dacui.dll
rundll32.exe advpack.dll /DelNodeRunDLL32 C:\WINNT\Catroot\icatalog.mdb
regsvr32 setupwbv.dll /s
regsvr32 wininet.dll /s
regsvr32 comcat.dll /s
regsvr32 shdoc401.dll /s
regsvr32 shdoc401.dll /i /s
regsvr32 asctrls.ocx /s
regsvr32 oleaut32.dll /s
regsvr32 shdocvw.dll /I /s
regsvr32 shdocvw.dll /s
regsvr32 browseui.dll /s
regsvr32 browseui.dll /I /s
regsvr32 msrating.dll /s
regsvr32 mlang.dll /s
regsvr32 hlink.dll /s
regsvr32 mshtml.dll /s
regsvr32 mshtmled.dll /s
regsvr32 urlmon.dll /s
regsvr32 plugin.ocx /s
regsvr32 sendmail.dll /s
regsvr32 comctl32.dll /i /s
regsvr32 inetcpl.cpl /i /s
regsvr32 mshtml.dll /i /s
regsvr32 scrobj.dll /s
regsvr32 mmefxe.ocx /s
regsvr32 proctexe.ocx mshta.exe /s
regsvr32 corpol.dll /s
regsvr32 jscript.dll /s
regsvr32 msxml.dll /s
regsvr32 imgutil.dll /s
regsvr32 thumbvw.dll /s
regsvr32 cryptext.dll /s
regsvr32 rsabase.dll /s
regsvr32 triedit.dll /s
regsvr32 dhtmled.ocx /s
regsvr32 inseng.dll /s
regsvr32 iesetup.dll /i /s
regsvr32 hmmapi.dll /s
regsvr32 cryptdlg.dll /s
regsvr32 actxprxy.dll /s
regsvr32 dispex.dll /s
regsvr32 occache.dll /s
regsvr32 occache.dll /i /s
regsvr32 iepeers.dll /s
regsvr32 wininet.dll /i /s
regsvr32 urlmon.dll /i /s
regsvr32 digest.dll /i /s
regsvr32 cdfview.dll /s
regsvr32 webcheck.dll /s
regsvr32 mobsync.dll /s
regsvr32 pngfilt.dll /s
regsvr32 licmgr10.dll /s
regsvr32 icmfilter.dll /s
regsvr32 hhctrl.ocx /s
regsvr32 inetcfg.dll /s
regsvr32 trialoc.dll /s
regsvr32 tdc.ocx /s
regsvr32 MSR2C.DLL /s
regsvr32 msident.dll /s
regsvr32 msieftp.dll /s
regsvr32 xmsconf.ocx /s
regsvr32 ils.dll /s
regsvr32 msoeacct.dll /s
regsvr32 wab32.dll /s
regsvr32 wabimp.dll /s
regsvr32 wabfind.dll /s
regsvr32 oemiglib.dll /s
regsvr32 directdb.dll /s
regsvr32 inetcomm.dll /s
regsvr32 msoe.dll /s
regsvr32 oeimport.dll /s
regsvr32 msdxm.ocx /s
regsvr32 dxmasf.dll /s
regsvr32 laprxy.dll /s
regsvr32 l3codecx.ax /s
regsvr32 acelpdec.ax /s
regsvr32 mpg4ds32.ax /s
regsvr32 voxmsdec.ax /s
regsvr32 danim.dll /s
regsvr32 Daxctle.ocx /s
regsvr32 lmrt.dll /s
regsvr32 datime.dll /s
regsvr32 dxtrans.dll /s
regsvr32 dxtmsft.dll /s
regsvr32 vgx.dll /s
regsvr32 WEBPOST.DLL /s
regsvr32 WPWIZDLL.DLL /s
regsvr32 POSTWPP.DLL /s
regsvr32 CRSWPP.DLL /s
regsvr32 FTPWPP.DLL /s
regsvr32 FPWPP.DLL /s
regsvr32 FLUPL.OCX /s
regsvr32 wshom.ocx /s
regsvr32 wshext.dll /s
regsvr32 vbscript.dll /s
regsvr32 scrrun.dll mstinit.exe /setup /s
regsvr32 msnsspc.dll /SspcCreateSspiReg /s
regsvr32 msapsspc.dll /SspcCreateSspiReg /s

rem =====解决IE不支持javascript之批处理法结束========

把上面保存为.bat文件后运行,不管提示什么一直确认下去,结束后重启,一切恢复正常.可以解决ie被破坏的很多问题,很管用!

点击这里下载此文件ie.bat

iframe 自适应高度代码

法一:
<script type="text/javascript">
function reinitIframe(){
var iframe = window.parent.document.getElementById("main-frame");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height =  height;
}catch (ex){}
}

reinitIframe();
</script>

其中main-frame为子框架的id,上面的代码放在被包含的框架底部即可

说明:如果想在主框架里进行控制的话,只需要将上面的
var iframe = window.parent.document.getElementById("main-frame");
修改为:
var iframe = document.getElementById("main-frame");
即可.
演示地址:http://ued.koubei.com/wp-content/iframe_a.html

法二:
<iframe id="iFrame1" name="iFrame1" width="100%" onload="this.height=iFrame1.document.body.scrollHeight" frameborder="0" src="index.htm"></iframe>
看到了吧,关键就在于onload="this.height=iFrame1.document.body.scrollHeight"!

这个方法本人没有测试,直接用的是第一种方法的.