Skip to content

Instantly share code, notes, and snippets.

@PeWe79
Forked from Moonbase59/iframe-resizer.html
Created December 25, 2023 17:43
Show Gist options
  • Save PeWe79/13ba5ae324dc8cafe0ec893d72819544 to your computer and use it in GitHub Desktop.
Save PeWe79/13ba5ae324dc8cafe0ec893d72819544 to your computer and use it in GitHub Desktop.
Responsive AzuraCast Embed Widget iframes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Iframe widget resizing test</title>
</head>
<body>
<h1>Iframe widget resizing test</h1>
<!-- Show what’s happening -->
<p>Window width: <span id="ww"></span>, height: <span id="wh"></span></p>
<p>Iframe width: <span id="iw"></span>, height: <span id="ih"></span></p>
<!-- Put embed widget code from station here, remove its "min-height" styling -->
<!-- Use id="widget" to simplify test -->
<!-- We use the "Request" widget as an example: switch it between 10/25 items/page -->
<iframe id="widget" src="https://demo.azuracast.com/public/azuratest_radio/embed-requests?theme=light" frameborder="0" allowtransparency="true" style="width: 100%; border: 0;"></iframe>
<hr/>
<p>Some page footer here.</p>
<script>
// wrap in an IIFE so not to pollute namespaces
(function () {
let iframe = document.querySelector("#widget");
let ww = document.querySelector("#ww");
let wh = document.querySelector("#wh");
let iw = document.querySelector("#iw");
let ih = document.querySelector("#ih");
// initial display
ww.textContent = window.innerWidth;
wh.textContent = window.innerHeight;
iw.textContent = iframe.clientWidth;
ih.textContent = iframe.clientHeight;
// AzuraCast widgets give us a postMessage containing their actual height and width
// starting at Rolling Release #8b15a55 (2023-12-21 23:00)
window.addEventListener('message', function(e) {
// message passed from iframe page has .height and .width
let message = e.data;
// only adjust height here, we have set "width: 100%;" on iframe
iframe.style.height = message.height + 'px';
//iframe.style.width = message.width + 'px';
// show it
ih.textContent = iframe.clientHeight;
});
// handle window resize
window.addEventListener("resize", function(e) {
console.log("Resize: width", window.innerWidth, "height", window.innerHeight);
ww.textContent = window.innerWidth;
wh.textContent = window.innerHeight;
iw.textContent = iframe.clientWidth;
ih.textContent = iframe.clientHeight;
});
// end wrapper
})();
</script>
</body>
</html>
<script>
// AzuraCast widgets give us a postMessage containing their actual height and width
// starting at Rolling Release #8b15a55 (2023-12-21 23:00)
// wrap in an IIFE so not to pollute namespaces
(function () {
let iframe = document.querySelector("#widget");
window.addEventListener('message', function(e) {
// message passed from iframe page has .height and .width
let message = e.data;
// only adjust height here, we have set "width: 100%;" on iframe
iframe.style.height = message.height + 'px';
//iframe.style.width = message.width + 'px';
});
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment