add script to resize iframe - jscancer - Javascript crap (relatively small)
(HTM) git clone git://git.codemadness.org/jscancer
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 9ba8da523384de2bdf895531b8145f93441ceec9
(DIR) parent ba476d77a16b011c02195bcd05570f4bc0886c47
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 30 Oct 2023 18:33:06 +0100
add script to resize iframe
Diffstat:
A resizeframe/example.html | 38 +++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/resizeframe/example.html b/resizeframe/example.html
@@ -0,0 +1,38 @@
+<h1>Iframe</h1>
+
+<p>
+Embed an iframe and scale its width, height or both so there are less scrollbars.
+Note that this requires serving from the same domain or adjusting the content security policies.
+</p>
+
+<iframe id="someid" class="autoresize-h" src="/" frameborder="0" border="0" width="960" height="100%"></iframe>
+
+<script type="text/javascript">
+// auto-resize iframes to fit its inner content: reduce scrollbars.
+var iframes = document.getElementsByTagName("IFRAME");
+for (var i = 0; i < iframes.length; i++) {
+ var iframe = iframes[i];
+ if (!iframe.classList.contains("autoresize-w") &&
+ !iframe.classList.contains("autoresize-h"))
+ continue;
+
+ // generate / bind element to function.
+ var fn = (function(el, rw, rh) {
+ return function(e) {
+ try {
+ var doc = this.contentWindow.document;
+ var width = doc.body.clientWidth || 0;
+ var height = doc.body.clientHeight || 0;
+ if (rw && width > 0)
+ el.width = width;
+ if (rh && height > 0)
+ el.height = height;
+ } catch(ex) {
+ }
+ };
+ })(iframe, iframe.classList.contains("autoresize-w"),
+ iframe.classList.contains("autoresize-h"));
+
+ iframe.addEventListener("load", fn, false);
+}
+</script>