Here's a JavaScript widget that embeds the form from the provided URL and auto-adjusts its height:
To use this widget, add this to your HTML:
<div id="form-container" data-form-widget></div>
<script src="form-widget.js"></script>
<div id="form-container" data-form-widget></div>
<script src="form-widget.js"></script>
I'll provide a simpler and more reliable solution using an iframe with JavaScript to auto-resize the height. Here's the working implementation:
<!DOCTYPE html>
<html>
<head>
<title>Embedded Form</title>
<style>
#form-container {
width: 100%;
border: none;
overflow: hidden;
}
</style>
</head>
<body>
<div id="form-container">
<iframe id="form-iframe" src="https://radiance.al/form/contatti/index.php" width="100%" frameborder="0" scrolling="no"></iframe>
</div>
<script>
// Function to adjust iframe height based on content
function resizeIframe() {
const iframe = document.getElementById('form-iframe');
try {
const doc = iframe.contentDocument || iframe.contentWindow.document;
iframe.style.height = doc.body.scrollHeight + 'px';
} catch (e) {
console.log('Error resizing iframe:', e);
}
}
// Resize when iframe loads and periodically to catch dynamic content changes
document.getElementById('form-iframe').onload = function() {
resizeIframe();
// Check for height changes every 500ms
setInterval(resizeIframe, 500);
};
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Embedded Form</title>
<style>
#form-container {
width: 100%;
border: none;
overflow: hidden;
}
</style>
</head>
<body>
<div id="form-container">
<iframe id="form-iframe" src="https://radiance.al/form/contatti/index.php" width="100%" frameborder="0" scrolling="no"></iframe>
</div>
<script>
// Function to adjust iframe height based on content
function resizeIframe() {
const iframe = document.getElementById('form-iframe');
try {
const doc = iframe.contentDocument || iframe.contentWindow.document;
iframe.style.height = doc.body.scrollHeight + 'px';
} catch (e) {
console.log('Error resizing iframe:', e);
}
}
// Resize when iframe loads and periodically to catch dynamic content changes
document.getElementById('form-iframe').onload = function() {
resizeIframe();
// Check for height changes every 500ms
setInterval(resizeIframe, 500);
};
</script>
</body>
</html>
To use this:
form-embed.html)Note: For this to work properly, the form page must be on the same domain as your page (due to same-origin policy). If you're embedding a form from a different domain, you'll need to:
window.postMessage()If you need a cross-domain solution, please let me know and I can provide an alternative approach.
Got it! Since you have control over both the parent page and the form page, we can implement a robust solution using postMessage for cross-origin communication. Here's the complete solution:
<!DOCTYPE html>
<html>
<head>
<title>Embedded Form</title>
<style>
#form-container {
width: 100%;
border: none;
overflow: hidden;
}
</style>
</head>
<body>
<div id="form-container">
<iframe id="form-iframe" src="https://radiance.al/form/contatti/index.php" width="100%" frameborder="0" scrolling="no"></iframe>
</div>
<script>
// Function to handle messages from the iframe
function handleMessage(event) {
if (event.origin !== "https://radiance.al") return; // Security check
if (event.data.type === 'resize') {
document.getElementById('form-iframe').style.height = event.data.height + 'px';
}
}
// Listen for messages from the iframe
window.addEventListener('message', handleMessage, false);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Embedded Form</title>
<style>
#form-container {
width: 100%;
border: none;
overflow: hidden;
}
</style>
</head>
<body>
<div id="form-container">
<iframe id="form-iframe" src="https://radiance.al/form/contatti/index.php" width="100%" frameborder="0" scrolling="no"></iframe>
</div>
<script>
// Function to handle messages from the iframe
function handleMessage(event) {
if (event.origin !== "https://radiance.al") return; // Security check
if (event.data.type === 'resize') {
document.getElementById('form-iframe').style.height = event.data.height + 'px';
}
}
// Listen for messages from the iframe
window.addEventListener('message', handleMessage, false);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
<style>
/* Your existing form styles here */
</style>
</head>
<body>
<!-- Your existing form content here -->
<script>
// Function to calculate and send height to parent
function sendHeight() {
const height = document.body.scrollHeight;
window.parent.postMessage({
type: 'resize',
height: height
}, 'https://your-parent-domain.com'); // Replace with your parent domain
}
// Send height on load and when content changes
window.addEventListener('load', sendHeight);
window.addEventListener('resize', sendHeight);
// If your form has dynamic content, observe mutations
const observer = new MutationObserver(sendHeight);
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
<style>
/* Your existing form styles here */
</style>
</head>
<body>
<!-- Your existing form content here -->
<script>
// Function to calculate and send height to parent
function sendHeight() {
const height = document.body.scrollHeight;
window.parent.postMessage({
type: 'resize',
height: height
}, 'https://your-parent-domain.com'); // Replace with your parent domain
}
// Send height on load and when content changes
window.addEventListener('load', sendHeight);
window.addEventListener('resize', sendHeight);
// If your form has dynamic content, observe mutations
const observer = new MutationObserver(sendHeight);
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
</script>
</body>
</html>
postMessage with origin checking to prevent security issueshttps://your-parent-domain.com with your actual domainThis solution will ensure the iframe always matches