alternative to using iframe for this HTML <main class="flex vert"> <div…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
alternative to using iframe for this
HTML
<div class="canvas-info flex">
<div>
Zoom level: <span id="zoom-level">1</span> (Zoom uses cursor position, not image or canvas center)
</div>
<div class="flex">
<div id="mouse-pos"> </div>
<div id="transformed-mouse-pos"> </div>
</div>
</div>
<div class="canvas-controls flex">
<button id="panLeft" title="Pan left"><img class="icon" src="https://svgshare.com/i/xbd.svg#pan-left" alt="Pan left"></button>
<button id="panRight" title="Pan right"><img class="icon" src="https://svgshare.com/i/xbd.svg#pan-right" alt="Pan right"></button>
<button id="panUp" title="Pan up"><img class="icon" src="https://svgshare.com/i/xbd.svg#pan-up" alt="Pan up"></button>
<button id="panDown" title="Pan down"><img class="icon" src="https://svgshare.com/i/xbd.svg#pan-down" alt="Pan down"></button>
<button id="zoomOut" title="Zoom out"><img class="icon" src="https://svgshare.com/i/xbd.svg#zoom-out" alt="Zoom out"></button>
<button id="zoomIn" title="Zoom in"><img class="icon" src="https://svgshare.com/i/xbd.svg#zoom-in" alt="Zoom in"></button>
<button id="center" title="Center"><img class="icon" src="https://svgshare.com/i/xbd.svg#center" alt="Center"></button>
<button id="reset" title="Reset"><img class="icon" src="https://svgshare.com/i/xbd.svg#reset" alt="Reset"></button>
<button id="rotateLeft" title="Rotate left"><img class="icon" src="https://svgshare.com/i/xbd.svg#rotate-left" alt="Rotate left"></button>
<button id="rotateRight" title="Rolotate right"><img class="icon" src="https://svgshare.com/i/xbd.svg#rotate-right" alt="Rotate right"></button>
<button id="mirror" title="Mirror"><img class="icon" src="https://svgshare.com/i/xbd.svg#mirror" alt="Mirror"></button>
<button id="flip" title="Flip"><img class="icon" src="https://svgshare.com/i/xbd.svg#flip" alt="Flip"></button>
</div>
CSS
body { width: 100%; height: 100%; padding: 10px; }
main { width: calc(100vw - 20px); height: calc(100vh - 20px); padding: 10px; overflow: hidden; border: 1px solid #333; background: #eee; }
.flex { display: flex; align-items: center; gap: 5px; }
.vert { flex-direction: column; justify-content: center; }
.canvas-container { flex-direction: column; border: 2px solid #333; padding: 10px; }
canvas { border: 1px dotted #333; background: #777; cursor: crosshair; }
.canvas-info { flex-direction: column; font-size: smaller; }
.canvas-controls { padding: 5px; }
.canvas-controls button span { display: inline-block; width: 20px; }
.rotateR { transform: rotate(90deg); }
.rotateL { transform: rotate(-90deg); }
.icon { width: 24px; height: 24px; }
JS
const IMGURL = "https://i.imgur.com/Z5mPZ2Y.jpg";
const ZOOMINLEVEL = 1.2; const ZOOMOUTLEVEL = 0.8; const MINZOOMLEVEL = 0.1; const MAXZOOMLEVEL = 15; const CANVASWITH = 500; const CANVASHEIGHT = 325; const PANSTEP = 20; const DEFAULTCURSOR = "crosshair"; const MOUSEDOWNCURSOR = "grab"; const ZOOMINCURSOR = "zoom-in"; const ZOOMOUTCURSOR = "zoom-out"; const IMAGESMOOTHINGENABLED = false;
var currentZoomLevel = 1; var isDragging = false; var dragStartPosition = { x: 0, y: 0 };
var canvas, context, image, currentTransformedCursor, scaledImgWidth, scaledImgHeight, startX, startY, canvasCenterX, canvasCenterY, zoomLevelTxt, mousePos, transformedMousePos, scale;
window.onload = () => { loadImage(); addEventListeners(); };
function loadImage() { canvas = document.getElementById("canvas"); canvas.width = CANVASWITH; canvas.height = CANVASHEIGHT; context = canvas.getContext("2d"); context.imageSmoothingEnabled = IMAGESMOOTHINGENABLED;
zoomLevelTxt = document.getElementById("zoom-level"); mousePos = document.getElementById("mouse-pos"); transformedMousePos = document.getElementById("transformed-mouse-pos");
image = new Image();
image.onload = function () { init(); reset(); }; image.src = IMGURL; }
function init() { scale = Math.min(canvas.width / image.width, canvas.height / image.height); scaledImgWidth = image.width * scale; scaledImgHeight = image.height * scale; startX = (canvas.width - scaledImgWidth) / 2; startY = (canvas.height - scaledImgHeight) / 2; canvasCenterX = canvas.width / 2; canvasCenterY = canvas.height / 2; canvas.style.cursor = DEFAULTCURSOR; context.save(); }
function drawImageToCanvas() { context.save(); context.setTransform(1, 0, 0, 1, 0, 0); context.clearRect(0, 0, canvas.width, canvas.height); context.restore(); context.drawImage(image, startX, startY, scaledImgWidth, scaledImgHeight); }
function getTransformedPoint(x, y) { const originalPoint = new DOMPoint(x, y); return context.getTransform().invertSelf().transformPoint(originalPoint); }
function onMouseDown(event) { isDragging = true; canvas.style.cursor = MOUSEDOWNCURSOR; dragStartPosition = getTransformedPoint(event.offsetX, event.offsetY); }
function onMouseMove(event) { canvas.style.cursor = DEFAULTCURSOR; currentTransformedCursor = getTransformedPoint(event.offsetX, event.offsetY);
displayMousePos(event.offsetX, event.offsetY); event.preventDefault();
if (isDragging) { pan( currentTransformedCursor.x - dragStartPosition.x, currentTransformedCursor.y - dragStartPosition.y ); } }
function onMouseUp() { isDragging = false; canvas.style.cursor = DEFAULTCURSOR; }
function onWheel(event) { zoom(event.deltaY < 0 ? ZOOMINLEVEL : ZOOMOUTLEVEL); event.preventDefault(); }
function zoom(zoomLevel) { if (currentZoomLevel == MINZOOMLEVEL && zoomLevel <= 1) { return; }
if (currentZoomLevel == MAXZOOMLEVEL && zoomLevel >= 1) { return; }
currentZoomLevel = Math.min( Math.max(currentZoomLevel * zoomLevel, MINZOOMLEVEL), MAXZOOMLEVEL ); displayZoomLevel();
canvas.style.cursor = zoomLevel > 1 ? ZOOMINCURSOR : ZOOMOUTCURSOR; context.translate(currentTransformedCursor.x, currentTransformedCursor.y); context.scale(zoomLevel, zoomLevel); context.translate(-currentTransformedCursor.x, -currentTransformedCursor.y); drawImageToCanvas(); }
function pan(x, y) { context.translate(x, y); drawImageToCanvas(); }
function rotate(degrees) { context.translate(canvasCenterX, canvasCenterY); context.rotate((Math.PI / 180) * degrees); context.translate(-canvasCenterX, -canvasCenterY); drawImageToCanvas(); }
function mirror() { context.translate(canvasCenterX, canvasCenterY); context.scale(-1, 1); context.translate(-canvasCenterX, -canvasCenterY); drawImageToCanvas(); }
function flip() { context.translate(canvasCenterX, canvasCenterY); context.scale(1, -1); context.translate(-canvasCenterX, -canvasCenterY); drawImageToCanvas(); }
function center() { drawImageToCanvas(true); }
function reset() { context.restore(); context.save(); drawImageToCanvas(); currentZoomLevel = 1; displayZoomLevel(); canvas.style.cursor = DEFAULTCURSOR; }
function addEventListeners() { canvas.addEventListener("mousedown", onMouseDown); canvas.addEventListener("mousemove", onMouseMove); canvas.addEventListener("mouseup", onMouseUp); canvas.addEventListener("wheel", onWheel); document.getElementById("reset").addEventListener("click", function () { reset(); }); document.getElementById("center").addEventListener("click", function () { //alert("Not implemnted (yet)"); center(); }); document.getElementById("zoomIn").addEventListener("click", function (event) { currentTransformedCursor = { x: canvasCenterX, y: canvasCenterY }; zoom(ZOOMINLEVEL); }); document .getElementById("zoomOut") .addEventListener("click", function (event) { currentTransformedCursor = { x: canvasCenterX, y: canvasCenterY }; zoom(ZOOMOUTLEVEL); }); document.getElementById("rotateRight").addEventListener("click", function () { rotate(90); }); document .getElementById("rotateLeft") .addEventListener("click", function (event) { rotate(-90); }); document.getElementById("mirror").addEventListener("click", function (event) { mirror(); }); document.getElementById("flip").addEventListener("click", function (event) { flip(); }); document .getElementById("panLeft") .addEventListener("click", function (event) { pan(-PANSTEP, 0); }); document .getElementById("panRight") .addEventListener("click", function (event) { pan(PANSTEP, 0); }); document.getElementById("panUp").addEventListener("click", function (event) { pan(0, -PANSTEP); }); document .getElementById("panDown") .addEventListener("click", function (event) { pan(0, PANSTEP); }); }
function displayMousePos(x, y) { var transX = currentTransformedCursor.x.toFixed(2); var transY = currentTransformedCursor.y.toFixed(2); mousePos.innerText = Original ({y}); transformedMousePos.innerText = Transformed ({transY}); }
function displayZoomLevel() { zoomLevelTxt.innerText = +currentZoomLevel.toFixed(2); }
Certainly! If you want to embed or display external content (like another HTML page, a web app, or a document) without using an <iframe>, your options depend on what you want to achieve and what the content is. Here are the most common alternatives and their use-cases:
If the external content is HTML or text (and CORS allows), you can fetch it and inject it into your page:
fetch('https://example.com/page.html')
.then(response => response.text())
.then(html => {
document.getElementById('target-div').innerHTML = html;
});
Note:
<object> or <embed>These tags can embed other documents, PDFs, SVGs, etc. Example:
<object data="https://example.com/page.html" width="600" height="400"></object>
or
<embed src="https://example.com/page.html" width="600" height="400">
Note:
If you control the server, you can proxy the external content through your backend, then fetch and inject it as in #1. This bypasses CORS issues.
If you want to show different "pages" or "views" in your app, use a SPA router (like React Router, Vue Router, etc.) to swap content dynamically without iframes.
If you want to encapsulate functionality, you can use Web Components to create reusable, isolated widgets.
If you want to display graphics or interactive content (like your example), use <canvas> or <svg> directly, as you are already doing.
need an iframe alternative for ignition perspective view, due to the site not being HTTPS
Great clarification! Ignition Perspective (from Inductive Automation) is a web-based industrial HMI/SCADA platform. Its "views" are typically embedded in other web pages using an <iframe>. However, if your main site is HTTPS and the Perspective gateway is HTTP, modern browsers will block the iframe due to mixed content restrictions.
You want to embed a Perspective view from a non-HTTPS (HTTP) Ignition server into your HTTPS site, but iframes are blocked.
<object>, <embed>, etc., are also blocked for the same reason.Set up a reverse proxy on your HTTPS site/server that forwards requests to the HTTP Ignition Perspective gateway.
https://yoursite.com/ignition/ (HTTPS).http://ignition-server/ (HTTP).https://yoursite.com/ignition/ safely.How to do it:
/ignition/ to your Ignition server.location /ignition/ {
proxy_pass http://IGNITION_SERVER_IP:8088/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
This is the only robust, secure, and browser-compatible solution.
If you need to communicate between windows or tabs, use window.open() and window.postMessage(). This is not for embedding, but for communication.
If the external content is data (not a full page), use their API to fetch data and render it in your own UI.
You are already not using an iframe. Your app is a self-contained canvas-based viewer.
If you want to load images or data from another domain, just use <img> or fetch APIs as you do.
If you want to embed this viewer in another page without an iframe, you can:
<my-canvas-viewer></my-canvas-viewer>).class CanvasViewer extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<canvas id="canvas"></canvas>
<!-- ...rest of your controls... -->
`;
// ...initialize your JS here, using this.querySelector('#canvas')...
}
}
customElements.define('canvas-viewer', CanvasViewer);
Then use in HTML:
<canvas-viewer></canvas-viewer>
| Use-case | Alternative to iframe |
|---|---|
| Embed HTML page | <object>, <embed>, fetch+inject (CORS) |
| Embed app/widget | Web Component, JS init in div |
| Embed image | <img>, <canvas> |
| Embed data | Fetch API, render yourself |
| Embed PDF/SVG | <object>, <embed> |
If you describe your specific use-case (what you want to embed, and where), I can give you a more targeted solution!
| Solution | Embeds Perspective View? | Secure? | Works in All Browsers? |
|---|---|---|---|
| Reverse Proxy | Yes | Yes | Yes |
| Upgrade Ignition to SSL | Yes | Yes | Yes |
| Open in New Tab | No (not embedded) | Yes | Yes |
| API + Custom UI | No (not embedded) | Yes | Yes |
| Disable Browser Block | Yes | No | No |
There is no HTML/JS alternative to iframes that will bypass browser mixed content restrictions.
You must either:
If you need help with a reverse proxy config, let me know your web server (Nginx, Apache, etc.) and I can provide a sample!