<script>
const links = {
link1: 'http://somesite.com',
}
let currentIframeId;
// top left red button to close the full page iframe
const closeBtn = document.createElement('button');
closeBtn.classList.add('close-btn')
closeBtn.onclick = handleCloseIFrame
closeBtn.innerText = 'close'
function handleLinkClick(index) {
currentIframeId = index;
const link = links[index]
const iframe = document.createElement('iframe')
iframe.src = link
iframe.width = '567'
iframe.height = '392'
iframe.classList.add('full-page-iframe', index)
document.body.appendChild(iframe)
setCloseBtVisibility(true)
}
// control the visibility of the close button
function setCloseBtVisibility(visible) {
if(visible) {
document.body.appendChild(closeBtn)
}else {
document.body.removeChild(closeBtn);
}
}
function handleCloseIFrame() {
const iframe = document.querySelector(`.${currentIframeId}`);
document.body.removeChild(iframe)
setCloseBtVisibility(false)
}
</script>
The issue lies in how the iframe and the closeBtn are being dynamically appended to the document's <body> rather than being appended as siblings to the "BBS Terminal" button (or its parent container). By default, appending elements to the <body> causes them to appear at the very end of the document.
To fix this and ensure that the iframe and closeBtn appear directly beneath the "BBS Terminal" button, you need to append the iframe and closeBtn to a container that's close to the button in the DOM hierarchy.
Sorry for the late reply. But, that doesn't seem to work either. Using const iframe = document.createElement('iframe') as a frame. You know what? I'll just leave it the way it is. If I get serious visiters to my website, that want to use the terminal, then so be it.. What if they don"? So be it.
The issue isn’t that you’re using this as your iframe, @ignatius.
const iframe = document.createElement('iframe')
The problem lies in how you’re attaching the iframe to the DOM. Specifically, your code attaches the iframe to the body element:
document.body.appendChild(iframe)
This means the iframe will appear at the bottom of the page, as the body represents the entire document.
To position the iframe as you intend, you need to attach it to the appropriate HTML element.
I shared links to basic DOM manipulation earlier so you can explore and understand this better. It’s not rocket science, @ignatius, but if you’re using JavaScript to manipulate DOM objects, it’s essential to grasp what the DOM is and how JavaScript interacts with it.
Something like this might work, instead of appending the iframe to the body (incorrectly):
closeBtn.append('iframe`)
However, I want you @ignatius to make the change and progress yourself, so I'm not going to rewrite your javascript completely. The required change is too simple does not require a lot of work if you understand the basics.
Also, do not forget to use the developers console in your browser. There, you can click on any element to get the DOM "address" (location), check JS vars, etc.