Rough draft of a new JQuery method

I have run into issues recently with the browsers’ implementation of the resize event on the window.  Opera, Safari, IE and Firefox all have different behaviors when this event is fired.

  1. Firefox only fires it when you release the mouse.
  2. IE fires this event many many times while resizing.
  3. Safari will fire continusouly while dragging
  4. Opera fires after the resizing stopped

You can read more about this here.

These differences led me to search for a solution.  I quickly wrote this proof of concept function today which may help solve this problem.  It is a JQuery add-on called resizeComplete.  It works by checking periodically after a resize starts and checks if the size of the object has changed.  Once no change is detected it invokes the call back method.

This has not been tested much at all but I think it might be on the right track.

jQuery.fn.resizeComplete = function(callback)
{
 
  var element = this;
  var height = element.height();
  var width = element.width();
  var monitoring = false;
  var timer;
  
  function monitorResizing()
  {
    monitoring = true;
    
    var newHeight = element.height();
    var newWidth = element.width();
    
    if(newHeight != height || newWidth != width)
    {
      height = newHeight;
      width = newWidth;
      timer = setTimeout(function() { monitorResizing() },100);
    }
    else
    {
      monitoring = false;
      clearTimeout(timer);
      callback();
    }
  }
  
  function onResize()
  {
    if(monitoring) return;
    monitorResizing();
  }
  
  element.resize(onResize);
  
}

Here is an example of its usage:

$(document).ready(function()
{
  $(window).resizeComplete(function(){ document.write('hi') });
});
This entry was posted in JavaScript, JQuery. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>