Created
August 17, 2011 10:47
-
-
Save think49/1151313 to your computer and use it in GitHub Desktop.
iframeAutoAdjust.js: iframeの高さをリンク先文書の高さにする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} | |
})(); |
Author
think49
commented
Aug 17, 2011
- iframe-auto-adjust.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment