JAVASCRIPT   51

DYNIFS

Guest on 7th July 2022 08:34:20 AM

  1. /*************************************************************\
  2.  *      DYNIFS - Dynamic IFrame Auto Size v1.0.0
  3.  *
  4.  *      Copyright (C) Markus (phpMiX)
  5.  *      This script is released under GPL License.
  6.  *      Feel free to use this script (or part of it) wherever you need
  7.  *      it ...but please, give credit to original author. Thank you. :-)
  8.  *      We will also appreciate any links you could give us.
  9.  *      http://www.phpmix.org
  10.  *
  11.  *      Enjoy! ;-)
  12. \*************************************************************/
  13.  
  14. var DYNIFS = {
  15.         // Storage for known IFrames.
  16.         iframes: {},
  17.         // Here we save any previously installed onresize handler.
  18.         oldresize: null,
  19.         // Flag that tell us if we have already installed our onresize handler.
  20.         ready: false,
  21.         // The document dimensions last time onresize was executed.
  22.         dim: [-1,-1],
  23.         // Timer ID used to defer the actual resize action.
  24.         timerID: 0,
  25.         // Obtain the dimensions (width,height) of the given document.
  26.         getDim: function(d) {
  27.                 var w=200, h=200, scr_h, off_h;
  28.                 if( d.height ) { return [d.width,d.height]; }
  29.                 with( d.body ) {
  30.                         if( scrollHeight ) { h=scr_h=scrollHeight; w=scrollWidth; }
  31.                         if( offsetHeight ) { h=off_h=offsetHeight; w=offsetWidth; }
  32.                         if( scr_h && off_h ) h=Math.max(scr_h, off_h);
  33.                 }
  34.                 return [w,h];
  35.         },
  36.         // This is our window.onresize handler.
  37.         onresize: function() {
  38.                 // Invoke any previously installed onresize handler.
  39.                 if( typeof this.oldresize == 'function' ) { this.oldresize(); }
  40.                 // Check if the document dimensions really changed.
  41.                 var dim = this.getDim(document);
  42.                 if( this.dim[0] == dim[0] && this.dim[1] == dim[1] ) return;
  43.                 // Defer the resize action to prevent endless loop in quirksmode.
  44.                 if( this.timerID ) return;
  45.                 this.timerID = setTimeout('DYNIFS.deferred_resize();', 10);
  46.         },
  47.         // This is where the actual IFrame resize is invoked.
  48.         deferred_resize: function() {
  49.                 // Walk the list of known IFrames to see if they need to be resized.
  50.                 for( var id in this.iframes ) this.resize(id);
  51.                 // Store resulting document dimensions.
  52.                 this.dim = this.getDim(document);
  53.                 // Clear the timer flag.
  54.                 this.timerID = 0;
  55.         },
  56.         // This is invoked when the IFrame is loaded or when the main window is resized.
  57.         resize: function(id) {
  58.                 // Browser compatibility check.
  59.                 if( !window.frames || !window.frames[id] || !document.getElementById || !document.body )
  60.                         return;
  61.                 // Get references to the IFrame window and layer.
  62.                 var iframe = window.frames[id];
  63.                 var div = document.getElementById(id);
  64.                 if( !div ) return;
  65.                 // Save the IFrame id for later use in our onresize handler.
  66.                 if( !this.iframes[id] ) {
  67.                         this.iframes[id] = true;
  68.                 }
  69.                 // Should we inject our onresize event handler?
  70.                 if( !this.ready ) {
  71.                         this.ready = true;
  72.                         this.oldresize = window.onresize;
  73.                         window.onresize = new Function('DYNIFS.onresize();');
  74.                 }
  75.                 // This appears to be necessary in MSIE to compute the height
  76.                 // when the IFrame'd document is in quirksmode.
  77.                 // OTOH, it doesn't seem to break anything in standards mode, so...
  78.                 if( document.all ) div.style.height = '0px';
  79.                 // Resize the IFrame container.
  80.                 var dim = this.getDim(iframe.document);
  81.                 div.style.height = (dim[1]+30) + 'px';
  82.         }
  83. };

Raw Paste


Login or Register to edit or fork this paste. It's free.