Skip to content

Instantly share code, notes, and snippets.

@think49
Created August 17, 2011 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/1151313 to your computer and use it in GitHub Desktop.
Save think49/1151313 to your computer and use it in GitHub Desktop.
iframeAutoAdjust.js: iframeの高さをリンク先文書の高さにする
/**
* iframe-auto-adjust.js
*
* @version 1.0.1
* @author think49
* @url https://gist.github.com/1151313
*/
'use strict';
(function () {
function handleIframeLoad (event) {
var iframe, iframeDoc, docWidth, docHeight, scrollWidth, scrollHeight, style;
iframe = event.target || event.srcElement;
// RFC3986 (3.1. Scheme)
if (/^[A-Za-z](?:[A-Za-z]|[0-9]|[+\-.])*:\u002F\u002F/.test(iframe.getAttribute('src'))) {
return;
}
iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
if (!iframeDoc) {
return;
}
docWidth = iframeDoc.width;
docHeight = iframeDoc.height;
scrollWidth = iframeDoc.documentElement.scrollWidth;
scrollHeight = iframeDoc.documentElement.scrollHeight;
style = iframe.style;
style.width = (docWidth && docWidth >= scrollWidth ? docWidth : scrollWidth + 15) + 'px';
style.height = (docHeight && docHeight >= scrollHeight ? docHeight : scrollHeight + 15) + 'px';
}
function handleDOMContentLoaded (event) {
var doc, iframes, iframe, i, l;
doc = event.target || document;
iframes = doc.getElementsByTagName('iframe');
for (i = 0, l = iframes.length; i < l; i++) {
iframe = iframes[i];
if (typeof iframe.addEventListener === 'function') {
iframe.addEventListener('load', handleIframeLoad, false);
} else {
handleIframeLoad.call(iframe, {target: iframe});
}
}
}
if (typeof document.addEventListener === 'function') {
document.addEventListener ('DOMContentLoaded', handleDOMContentLoaded, false);
} else if (typeof attachEvent === 'object' || typeof attachEvent === 'function') {
attachEvent('onload', handleDOMContentLoaded);
}
})();
@think49
Copy link
Author

think49 commented Aug 17, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment