javascript – jQuery DOMWindow脚本不释放内存

编程之家收集整理的这篇文章主要介绍了javascript – jQuery DOMWindow脚本不释放内存编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用我发现的jQuery脚本
http://swip.codylindley.com/DOMWindowDemo.html
在我的网站上,当访问者点击链接时创建一个灯箱/ domwindow弹出窗口.

不幸的是,当用户关闭dom窗口时,脚本似乎没有释放内存.如果用户多次打开和关闭窗口,则会导致页面显着减慢并使用户的浏览器崩溃.

这是上面网站的jQuery脚本:

(function($){

    //closeDOMWindow
    $.fn.closeDOMWindow = function(settings){

        if(!settings){settings={};}

        var run = function(passingThis){

            if(settings.anchoredClassName){
                var $anchorClassName = $('.'+settings.anchoredClassName);
                $anchorClassName.fadeOut('fast',function(){
                    if($.fn.draggable){
                        $anchorClassName.draggable('destory').trigger("unload").remove();   
                    }else{
                        $anchorClassName.trigger("unload").remove();
                    }
                });
                if(settings.functionCallOnClose) {
                    settings.functionCallAfterClose();
                }
            }else{
                var $DOMWindowOverlay = $('#DOMWindowOverlay');
                var $DOMWindow = $('#DOMWindow');
                $DOMWindowOverlay.fadeOut('fast',function(){
                    $DOMWindowOverlay.trigger('unload').unbind().remove();                                                                    
                });
                $DOMWindow.fadeOut('fast',function(){
                    if($.fn.draggable){
                        $DOMWindow.draggable("destroy").trigger("unload").remove();
                    }else{
                        $DOMWindow.trigger("unload").remove();
                    }
                });

                $(window).unbind('scroll.DOMWindow');
                $(window).unbind('resize.DOMWindow');

                if($.fn.openDOMWindow.isIE6){$('#DOMWindowIE6FixIframe').remove();}
                if(settings.functionCallOnClose){settings.functionCallAfterClose();}
            }   
        };

        if(settings.eventType){//if used with $().
            return this.each(function(index){
                $(this).bind(settings.eventType, function(){
                    run(this);
                    return false;
                });
            });
        }else{//else called as $.function
            run();
        }

    };

    //allow for public call, pass settings
    $.closeDOMWindow = function(s){$.fn.closeDOMWindow(s);};

    //openDOMWindow
    $.fn.openDOMWindow = function(instanceSettings){    

        var shortcut =  $.fn.openDOMWindow;

        //default settings combined with callerSettings////////////////////////////////////////////////////////////////////////

        shortcut.defaultsSettings = {
            anchoredClassName:'',
            anchoredSelector:'',
            borderColor:'#ccc',
            borderSize:'4',
            draggable:0,
            eventType:null, //click, blur, change, dblclick, error, focus, load, mousedown, mouSEOut, mouseup etc...
            fixedWindowY:100,
            functionCallOnOpen:null,
            functionCallOnClose:null,
            height:500,
            loader:0,
            loaderHeight:0,
            loaderImagePath:'',
            loaderWidth:0,
            modal:0,
            overlay:1,
            overlayColor:'#000',
            overlayOpacity:'85',
            positionLeft:0,
            positionTop:0,
            positionType:'centered', // centered, anchored, absolute, fixed
            width:500, 
            windowBGColor:'#fff',
            windowBGImage:null, // http path
            windowHTTPType:'get',
            windowPadding:10,
            windowSource:'inline', //inline, ajax, iframe
            windowSourceID:'',
            windowSourceURL:'',
            windowSourceAttrURL:'href'
        };

        var settings = $.extend({}, $.fn.openDOMWindow.defaultsSettings , instanceSettings || {});

        //Public functions

        shortcut.viewPortHeight = function(){ return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;};
        shortcut.viewPortWidth = function(){ return self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;};
        shortcut.scrollOffsetHeight = function(){ return self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;};
        shortcut.scrollOffsetWidth = function(){ return self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;};
        shortcut.isIE6 = typeof document.body.style.maxHeight === "undefined";

        //Private Functions/////////////////////////////////////////////////////////////////////////////////////////////////////////

        var sizeOverlay = function(){
            var $DOMWindowOverlay = $('#DOMWindowOverlay');
            if(shortcut.isIE6){//if IE 6
                var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 4;
                var overlayViewportWidth = document.documentElement.offsetWidth - 21;
                $DOMWindowOverlay.css({'height':overlayViewportHeight +'px','width':overlayViewportWidth+'px'});
            }else{//else Firefox, safari, opera, IE 7+
                $DOMWindowOverlay.css({'height':'100%','width':'100%','position':'fixed'});
            }   
        };

        var sizeIE6Iframe = function(){
            var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 4;
            var overlayViewportWidth = document.documentElement.offsetWidth - 21;
            $('#DOMWindowIE6FixIframe').css({'height':overlayViewportHeight +'px','width':overlayViewportWidth+'px'});
        };

        var centerDOMWindow = function() {
            var $DOMWindow = $('#DOMWindow');
            if(settings.height + 50 > shortcut.viewPortHeight()){//added 50 to be safe
                $DOMWindow.css('left',Math.round(shortcut.viewPortWidth()/2) + shortcut.scrollOffsetWidth() - Math.round(($DOMWindow.outerWidth())/2));
            }else{
                $DOMWindow.css('left',Math.round(shortcut.viewPortWidth()/2) + shortcut.scrollOffsetWidth() - Math.round(($DOMWindow.outerWidth())/2));
                $DOMWindow.css('top',Math.round(shortcut.viewPortHeight()/2) + shortcut.scrollOffsetHeight() - Math.round(($DOMWindow.outerHeight())/2));
            }
        };

        var centerLoader = function() {
            var $DOMWindowLoader = $('#DOMWindowLoader');
            if(shortcut.isIE6){//if IE 6
                $DOMWindowLoader.css({'left':Math.round(shortcut.viewPortWidth()/2) + shortcut.scrollOffsetWidth() - Math.round(($DOMWindowLoader.innerWidth())/2),'position':'absolute'});
                $DOMWindowLoader.css({'top':Math.round(shortcut.viewPortHeight()/2) + shortcut.scrollOffsetHeight() - Math.round(($DOMWindowLoader.innerHeight())/2),'position':'absolute'});
            }else{
                $DOMWindowLoader.css({'left':'50%','top':'50%','position':'fixed'});
            }

        };

        var fixedDOMWindow = function(){
            var $DOMWindow = $('#DOMWindow');
            $DOMWindow.css('left', settings.positionLeft + shortcut.scrollOffsetWidth());
            $DOMWindow.css('top', + settings.positionTop + shortcut.scrollOffsetHeight());
        };

        var showDOMWindow = function(instance){
            if(arguments[0]){
                $('.'+instance+' #DOMWindowLoader').remove();
                $('.'+instance+' #DOMWindowContent').fadeIn('fast',function(){if(settings.functionCallOnOpen){settings.functionCallOnOpen();}});
                $('.'+instance+ '.closeDOMWindow').click(function(){
                    $.closeDOMWindow(); 
                    return false;
                });
            }else{
                $('#DOMWindowLoader').remove();
                $('#DOMWindow').fadeIn('fast',function(){if(settings.functionCallOnOpen){settings.functionCallOnOpen();}});
                $('#DOMWindow .closeDOMWindow').click(function(){                       
                    $.closeDOMWindow();
                    return false;
                });
            }

        };

        var urlQueryToObject = function(s){
              var query = {};
              s.replace(/b([^&=]*)=([^&=]*)b/g, function (m, a, d) {
                if (typeof query[a] != 'undefined') {
                  query[a] += ',' + d;
                } else {
                  query[a] = d;
                }
              });
              return query;
        };

        //Run Routine ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        var run = function(passingThis){

            //get values from element clicked, or assume its passed as an option
            settings.windowSourceID = $(passingThis).attr('href') || settings.windowSourceID;
            settings.windowSourceURL = $(passingThis).attr(settings.windowSourceAttrURL) || settings.windowSourceURL;
            settings.windowBGImage = settings.windowBGImage ? 'background-image:url('+settings.windowBGImage+')' : '';
            var urlOnly, urlQueryObject;

            if(settings.positionType == 'anchored'){//anchored DOM window

                var anchoredPositions = $(settings.anchoredSelector).position();
                var anchoredPositionX = anchoredPositions.left + settings.positionLeft;
                var anchoredPositionY = anchoredPositions.top + settings.positionTop;

                $('body').append('<div class="'+settings.anchoredClassName+'" style="'+settings.windowBGImage+';background-repeat:no-repeat;padding:'+settings.windowPadding+'px;overflow:auto;position:absolute;top:'+anchoredPositionY+'px;left:'+anchoredPositionX+'px;height:'+settings.height+'px;width:'+settings.width+'px;background-color:'+settings.windowBGColor+';border:'+settings.borderSize+'px solid '+settings.borderColor+';z-index:10001"><div id="DOMWindowContent" style="display:none"></div></div>');        
                //loader
                if(settings.loader && settings.loaderImagePath !== ''){
                    $('.'+settings.anchoredClassName).append('<div id="DOMWindowLoader" style="width:'+settings.loaderWidth+'px;height:'+settings.loaderHeight+'px;"><img src="'+settings.loaderImagePath+'" /></div>');

                }

                if($.fn.draggable){
                    if(settings.draggable){$('.' + settings.anchoredClassName).draggable({cursor:'move'});}
                }

                switch(settings.windowSource){
                    case 'inline'://////////////////////////////// inline //////////////////////////////////////////
                        $('.' + settings.anchoredClassName+" #DOMWindowContent").append($(settings.windowSourceID).children());
                        $('.' + settings.anchoredClassName).unload(function(){// move elements back when you're finished
                            $('.' + settings.windowSourceID).append( $('.' + settings.anchoredClassName+" #DOMWindowContent").children());              
                        });
                        showDOMWindow(settings.anchoredClassName);
                    break;
                    case 'iframe'://////////////////////////////// iframe //////////////////////////////////////////
                        $('.' + settings.anchoredClassName+" #DOMWindowContent").append('<iframe frameborder="0" hspace="0" wspace="0" src="'+settings.windowSourceURL+'" name="DOMWindowIframe'+Math.round(Math.random()*1000)+'" style="width:100%;height:100%;border:none;background-color:#fff;" class="'+settings.anchoredClassName+'Iframe" ></iframe>');
                        $('.'+settings.anchoredClassName+'Iframe').load(showDOMWindow(settings.anchoredClassName));
                    break;
                    case 'ajax'://////////////////////////////// ajax //////////////////////////////////////////    
                        if(settings.windowHTTPType == 'post'){

                            if(settings.windowSourceURL.indexOf("?") !== -1){//has a query string
                                urlOnly = settings.windowSourceURL.substr(0, settings.windowSourceURL.indexOf("?"));
                                urlQueryObject = urlQueryToObject(settings.windowSourceURL);
                            }else{
                                urlOnly = settings.windowSourceURL;
                                urlQueryObject = {};
                            }
                            $('.' + settings.anchoredClassName+" #DOMWindowContent").load(urlOnly,urlQueryObject,function(){
                                showDOMWindow(settings.anchoredClassName);
                            });
                        }else{
                            if(settings.windowSourceURL.indexOf("?") == -1){ //no query string, so add one
                                settings.windowSourceURL += '?';
                            }
                            $('.' + settings.anchoredClassName+" #DOMWindowContent").load(
                                settings.windowSourceURL + '&random=' + (new Date().getTime()),function(){
                                showDOMWindow(settings.anchoredClassName);
                            });
                        }
                    break;
                }

            }else{//centered, fixed, absolute DOM window

                //overlay & modal
                if(settings.overlay){
                    $('body').append('<div id="DOMWindowOverlay" style="z-index:10000;display:none;position:absolute;top:0;left:0;background-color:'+settings.overlayColor+';filter:alpha(opacity='+settings.overlayOpacity+');-moz-opacity: 0.'+settings.overlayOpacity+';opacity: 0.'+settings.overlayOpacity+';"></div>');
                    if(shortcut.isIE6){//if IE 6
                        $('body').append('<iframe id="DOMWindowIE6FixIframe"  src="blank.html"  style="width:100%;height:100%;z-index:9999;position:absolute;top:0;left:0;filter:alpha(opacity=0);"></iframe>');
                        sizeIE6Iframe();
                    }
                    sizeOverlay();
                    var $DOMWindowOverlay = $('#DOMWindowOverlay');
                    $DOMWindowOverlay.fadeIn('fast');
                    if(!settings.modal){$DOMWindowOverlay.click(function(){$.closeDOMWindow();});}
                }

                //loader
                if(settings.loader && settings.loaderImagePath !== ''){
                    $('body').append('<div id="DOMWindowLoader" style="z-index:10002;width:'+settings.loaderWidth+'px;height:'+settings.loaderHeight+'px;"><img src="'+settings.loaderImagePath+'" /></div>');
                    centerLoader();
                }

                //add DOMwindow
                $('body').append('<div id="DOMWindow" style="background-repeat:no-repeat;'+settings.windowBGImage+';overflow:auto;padding:'+settings.windowPadding+'px;display:none;height:'+settings.height+'px;width:'+settings.width+'px;background-color:'+settings.windowBGColor+';border:'+settings.borderSize+'px solid '+settings.borderColor+'; position:absolute;z-index:10001"></div>');

                var $DOMWindow = $('#DOMWindow');
                //centered, absolute, or fixed
                switch(settings.positionType){
                    case 'centered':
                        centerDOMWindow();
                        if(settings.height + 50 > shortcut.viewPortHeight()){//added 50 to be safe
                            $DOMWindow.css('top', (settings.fixedWindowY + shortcut.scrollOffsetHeight()) + 'px');
                        }
                    break;
                    case 'absolute':
                        $DOMWindow.css({'top':(settings.positionTop+shortcut.scrollOffsetHeight())+'px','left':(settings.positionLeft+shortcut.scrollOffsetWidth())+'px'});
                        if($.fn.draggable){
                            if(settings.draggable){$DOMWindow.draggable({cursor:'move'});}
                        }
                    break;
                    case 'fixed':
                        fixedDOMWindow();
                    break;
                    case 'anchoredSingleWindow':
                        var anchoredPositions = $(settings.anchoredSelector).position();
                        var anchoredPositionX = anchoredPositions.left + settings.positionLeft;
                        var anchoredPositionY = anchoredPositions.top + settings.positionTop;
                        $DOMWindow.css({'top':anchoredPositionY + 'px','left':anchoredPositionX+'px'});

                    break;
                }

                $(window).bind('scroll.DOMWindow',function(){
                    if(settings.overlay){sizeOverlay();}
                    if(shortcut.isIE6){sizeIE6Iframe();}
                    if(settings.positionType == 'centered'){centerDOMWindow();}
                    if(settings.positionType == 'fixed'){fixedDOMWindow();}
                });

                $(window).bind('resize.DOMWindow',function(){
                    if(shortcut.isIE6){sizeIE6Iframe();}
                    if(settings.overlay){sizeOverlay();}
                    if(settings.positionType == 'centered'){centerDOMWindow();}
                });

                switch(settings.windowSource){
                    case 'inline'://////////////////////////////// inline //////////////////////////////////////////
                        $DOMWindow.append($(settings.windowSourceID).children());
                        $DOMWindow.unload(function(){// move elements back when you're finished
                            $(settings.windowSourceID).append($DOMWindow.children());               
                        });
                        showDOMWindow();
                    break;
                    case 'iframe'://////////////////////////////// iframe //////////////////////////////////////////
                        $DOMWindow.append('<iframe frameborder="0" hspace="0" wspace="0" src="'+settings.windowSourceURL+'" name="DOMWindowIframe'+Math.round(Math.random()*1000)+'" style="width:100%;height:100%;border:none;background-color:#fff;" id="DOMWindowIframe" ></iframe>');
                        $('#DOMWindowIframe').load(showDOMWindow());
                    break;
                    case 'ajax'://////////////////////////////// ajax //////////////////////////////////////////
                        if(settings.windowHTTPType == 'post'){

                            if(settings.windowSourceURL.indexOf("?") !== -1){//has a query string
                                urlOnly = settings.windowSourceURL.substr(0, settings.windowSourceURL.indexOf("?"));
                                urlQueryObject = urlQueryToObject(settings.windowSourceURL);
                            }else{
                                urlOnly = settings.windowSourceURL;
                                urlQueryObject = {};
                            }
                            $DOMWindow.load(urlOnly,urlQueryObject,function(){
                                showDOMWindow();
                            });
                        }else{
                            if(settings.windowSourceURL.indexOf("?") == -1){ //no query string, so add one
                                settings.windowSourceURL += '?';
                            }
                            $DOMWindow.load(
                                settings.windowSourceURL + '&random=' + (new Date().getTime()),function(){
                                showDOMWindow();
                            });
                        }
                    break;
                }

            }//end if anchored, or absolute, fixed, centered

        };//end run()

        if(settings.eventType){//if used with $().
            return this.each(function(index){                 
                $(this).bind(settings.eventType,function(){
                    run(this);
                    return false;
                });
            }); 
        }else{//else called as $.function
            run();
        }

    };//end function openDOMWindow

    //allow for public call, pass settings
    $.openDOMWindow = function(s){$.fn.openDOMWindow(s);};

})(jQuery);

这是我的HTML中的超链接标记打开了灯箱.

<a href="/PHP/ajax/edit_map_pin.htm?mAddressBox0=FALSE&mPin_ID=foo" class="AjaxDOMWindow">Change Icon</a>

以下是siege的屏幕截图,详细说明了每次用户打开并从该链接关闭DOM窗口时内存步长的增加.任何帮助是极大的赞赏.谢谢!

解决方法:

问题是,remove()从文档树中删除节点,但它们仍然可用(例如,您可以再次使用它们并将它们放回到文档中).

在MSIE中,您可以将节点的outerHTML属性设置为空字符串以真正删除它们,在其他浏览器中我不确定如何.你可以看一下:http://www.josh-davis.org/node/7.

作者在那里使用了delete-statement,但我不确定它是否真的删除了节点.

总结

以上是编程之家为你收集整理的javascript – jQuery DOMWindow脚本不释放内存全部内容,希望文章能够帮你解决javascript – jQuery DOMWindow脚本不释放内存所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

相关文章

猜你在找的jQuery相关文章

目录验证码实现效果如下:生成验证码的方法:生成验证码随机背景颜色在html中引用完整源码下载地址:验证码实现效果如下:生成验证码的方法:function code_draw() { var canvas_width = $('#canvas').width(); var canvas_height = $('#canvas').height(); var canvas = document.getElementById("canvas"); //获取到canvas
比如info是一个字符串变量,如果需要去该变量的值,需要使用下面的语句:语法:alert($(&quot;#&quot;+id).val());例如:使用id设置隐藏控件 $(&quot;.img&quot;).mouseover(function(event){ var html=event.target.innerHTML; //alert(html); var aid=html.match(/&amp;lt;a id=&quot;(...
使用js去除字符串内所带有空格,有以下三种方法:( 1 ) replace正则匹配方法去除字符串内所有的空格:str = str.replace(/s*/g,&quot;&quot;);去除字符串内两头的空格:str = str.replace(/^s*|s*$/g,&quot;&quot;);去除字符串内左侧的空格:str = str.replace(/^s*/,&quot;&quot;);去除字符串内右侧的...
1.开启disabled,是input不可以编辑$(&quot;#input_id&quot;).attr(&quot;disabled&quot;,&quot;disabled&quot;);2.关闭disabled$(&quot;#input_id&quot;).removeAttr(&quot;disabled&quot;);普通js中是这样写的,document.getElementById(&quot;input_id&quot;).disabled = false;...
最近自学了一段时间的HTML、CSS、JavaScript、jQuery。通过自己这段时间学到的一点小知识,自己制作了一个小app------简易备忘录在此简单记录一下,同时也希望有更好创意的朋友提出改进意见或者指++++++++++++++++++++++++++++++++++++++++++++++++++++++++++开发工具:HBui
http://www.runoob.com/jquery/jquery-chaining.htmljQuery- 链(Chaining)通过jQuery,可以把动作/方法链接在一起。Chaining允许我们在一条语句中运行多个jQuery方法(在相同的元素上)。jQuery方法链接直到现在,我们都是一次写一条jQuery语句(一条接着另一条)。不过,有一
jQuery拥有以下滑动方法:slideDown(),slideUp(),slideToggle()1、jQueryslideDown()方法用于向下滑动元素。$(selector).slideDown(speed,callback);可选的speed参数规定效果的时长。它可以取以下值:"slow"、"fast"或毫秒。可选的callback参数是滑动完成后所执行的函数名称
一、宽度和高度获取宽度.width()描述:为匹配的元素集合中获取第一个元素的当前计算宽度值。这个方法不接受任何参数。.css(width) 和 .width()之间的区别是后者返回一个没有单位的数值(例如,400),前者是返回带有完整单位的字符串(例如,400px)。当一个元素的宽度需要数学计算的时候推